我试图使用聚合管道的$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.
}
]谢谢。
发布于 2022-10-05 11:09:57
您已经将$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
}
}
}
]
}
}
])https://stackoverflow.com/questions/73959497
复制相似问题