我试图统计与列表ID匹配的潜在客户数量,并将其与成本相结合。这是我的收藏:
[
{
"list_id": "5f76a53e8a93be772c14cc06",
"fname": "John",
"lname": "Doe",
"cost": 0
},
{
"list_id": "5f76a53e8a93be772c14cc06",
"fname": "John",
"lname": "Wayne",
"cost": 0.65
},
{
"list_id": "5f76a53e8a93be772c14cc06",
"fname": "John",
"lname": "Kelly",
"cost": 1
},
{
"list_id": "5f76a53e8a93be772c14cc06",
"fname": "John",
"lname": "Morrie",
"cost": 0.40
}
]
我试图得到这个结果:
[
{
"_id": null,
"lead_count": 4
"total_cost": 2.05
}
]
我的聚合看起来像这样:
db.collection.aggregate([
{
"$match": {
list_id: "5f76a53e8a93be772c14cc06"
}
},
{
"$group": {
_id: null,
total_cost: {
$sum: "$cost"
}
}
}
])
但它只给了我:
[
{
"_id": null,
"total_cost": 2.05
}
]
我需要如何调整它才能得到我想要的东西?以下是Playground链接:
https://mongoplayground.net/p/zvLY67Oa_Ps