我试图使用聚合管道的$facet阶段来匹配和分组文档,但我怀疑我的语法不正确。
有人能告诉我这个怎么了吗?继续获取消息";流水线阶段规范对象必须包含恰好一个字段";。
[
{
'$match': {
'Id': 59
}
}, {
'$unwind': {
'path': '$Vehicles'
}
}, {
// It's this stage that's playing up.
'$facet': {
'Offers': [
{
'$match': { 'Vehicles.VehicleGrouping': 'OOTW' },
'$group': {
'_id': '$Vehicles.Manufacturer',
'count': { '$sum': 1 }
}
}
]
}
// End of problematic stage.
}
]
谢谢。
您已经将$match
和$group
放在一个对象中,请尝试以下操作:
db.collection.aggregate([
{
"$match": {
"Id": 59
}
},
{
"$unwind": {
"path": "$Vehicles"
}
},
{
// It's this stage that's playing up.
"$facet": {
"Offers": [
{
"$match": {
"Vehicles.VehicleGrouping": "OOTW"
}
},
{
"$group": {
"_id": "$Vehicles.Manufacturer",
"count": {
"$sum": 1
}
}
}
]
}
}
])