Since you group by passCheck aslo, the result would be either success or failure. So whay=t you can do is, you can use a condition using $cond
{ $group: {
"_id": {
"year": {$substr: [ "$createdAt", 0, 4 ]},
"month": {$substr: [ "$createdAt", 5, 2 ]},
"day": {$substr: [ "$createdAt", 8, 2 ]}
},
"success_count": {
$sum: {
$cond: [{$eq: ["$passCheck", "success"]},1,0]
}
},
"failure_count": {
$sum: {
$cond: [ { $eq: ["$passCheck", "failure" ]},1,0]
}
}
}
},
{
"$addFields": {
"_id.failure_count": "$failure_count",
"_id.success_count": "$success_count",
"failure_count": "$$REMOVE",
"success_count": "$$REMOVE"
}
}
Working Mongo playground
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…