{
"_id" : ObjectId("593fbefd0b0cbbe593cd89d3"),
"coins" : "220",
"reasonId" : ObjectId("593500980b0cbbe593cd722c"),
"empId" : ObjectId("592eb2e224da300ca89b05d5"),
"clubId" : ObjectId("592d6bea7ab7f62d58001329")
}
{
"_id" : ObjectId("59413b170b0cbbe593cd8b46"),
"coins" : "220",
"reasonId" : ObjectId("593500980b0cbbe593cd722c"),
"empId" : ObjectId("592eb2e224da300ca89b05d5"),
"clubId" : ObjectId("592d6bea7ab7f62d58001329")
}
{
"_id" : ObjectId("593fbefd0b0cbbe593cd89d5"),
"reasonId" : ObjectId("5934ffb80b0cbbe593cd722a"),
"empId" : ObjectId("5912e5747f83352face7e431"),
"coins" : "100",
"clubId" : ObjectId("592d6bea7ab7f62d58001329")
}
我想根据clubId对单个empId的硬币求和。员工的硬币应仅基于一个俱乐部,如何对此进行聚合?
答案 0 :(得分:0)
让我们假设您的收藏品是"员工"但首先将硬币转换为字符串
db.employee.aggregate(
{
$match:{
"empId" : ObjectId("5912e5747f83352face7e431")
}
},
{
$group:{
_id: "$clubId",
sumCoins : {$sum: "$coins"}
}
}
)