【发布时间】:2021-10-02 17:03:58
【问题描述】:
我有简单的 mongo 集合:
{
...
customerProducts: [
{ organizationId: ObjectId('x') },
{ organizationId: ObjectId('y') },
{ organizationId: ObjectId('z') },
]
...
}
我需要过滤数组customerProducts 并使用organizationId = ObjectId('x') 保留元素。
我尝试通过$filter 进行操作。我的做法:
aggregation([{
$project: {
'customerProducts': {
$fiter: {
input: '$customerProducts',
as: 'item',
cond: { $eq: ['$$item.organizationId', ObjectId('x') }
}
}
}
}])
但 Compass 认为 - “Stage 必须是格式正确的文档”。我做错了什么?
【问题讨论】:
-
1) 操作员名称应该是
$filter而不是$fiter2) 缺少$eq的结束]数组括号=>cond: { $eq: ['$$item.organizationId', ObjectId('x')] } -
谢谢,但还是不行。
-
使用编辑器帮助您自动配对 [] 和 {},这很难手动完成。
标签: mongodb mongodb-query aggregation-framework