我的查询聚合为https://mongoplayground.net/p/jxaI7MAyBmJ
我通过在数据下添加阶段来过滤具有以下 2 个条件的项目类型、计数和 empName
db.collection.aggregate([
{
$facet: {
"data": [
{
$match: {
type: {
$in: [
"a1",
"a2"
]
}
}
},
{
$group: {
_id: {
emp_name: "$emp_name",
type: "$type"
},
count: {
$sum: 1
}
}
},
{
$project: {
empName: "$_id.emp_name",
type: "$_id.type",
count: 1,
_id: 0
}
}
//Have to ADD sub stages here with the above mentioned 2 condition
]
}
}
])
我尝试应用此条件,但无法解决此问题。
提前致谢
我仍然不明白你在寻找什么,但是这个给出了想要的结果:
db.collection.aggregate([
{
$facet: {
"data": [
{ $match: { type: "a2" } },
{
$group: {
_id: {
emp_name: "$emp_name",
type: "$type"
},
count: { $sum: 1 }
}
},
{
$project: {
empName: "$_id.emp_name",
type: "$_id.type",
count: 1,
_id: 0
}
}
]
}
}
])