能否请您告诉我如何计算状态为person
的总计p
。
这是我的代码
https://mongoplayground.net/p/d2Bmk4srq0O
db.collection.aggregate({
$group: {
_id: "$Department",
totalAttendance: {
$sum: "$Status"
},
}
})
我要计算状态为p
预期的输出。像这样的东西
[
{
"_id": "THBS",
"totalAttendance": 10
},
{
"_id": "HUAWEI",
"totalAttendance": 2
}
]
答案 0 :(得分:3)
db.collection.aggregate([
{ "$group": {
"_id": "$Department",
"totalAttendance": {
"$sum": {
"$cond": [
{ "$eq": [{ "$ltrim": { "input": "$Status" } }, "P "] },
1,
0
]
}
}
}}
])