如何通过特定ID过滤聚合管道
给定以下数据:
{
"_id": 1,
"name": "jim",
},
{
"_id": 2,
"name": "bob",
},
{
"_id": 3,
"name": "bill",
},
{
"_id": 4,
"name": "bob",
}
我有一个简单的聚合管道来检索名为 bob
的文档:
[
{ "$match": { "name": "bob" } }
]
但是,我还有一个 ids 数组(例如 [1, 2]
)我想用来过滤文档(在 $match
查询运行之前或之后)。因此,上述聚合管道的最终结果应该是:
{
"_id": 2,
"name": "bob",
}
通常我会使用 { "_id": { "$in": [1, 2] } }
这样的查询来执行此操作,但是通过 MongoDB 文档,聚合操作似乎与常规聚合操作不同,而且我似乎找不到可以实现此目的的聚合(无论是在文档中还是在网上)。
Given this data:
{
"_id": 1,
"name": "jim",
},
{
"_id": 2,
"name": "bob",
},
{
"_id": 3,
"name": "bill",
},
{
"_id": 4,
"name": "bob",
}
I have a simple aggregation pipeline to retrieve documents with the name bob
:
[
{ "$match": { "name": "bob" } }
]
However, I also have an array of ids (e.g. [1, 2]
) that I want to use to filter the documents (either before or after the $match
query runs). So the final result of the above aggregation pipeline should be:
{
"_id": 2,
"name": "bob",
}
Usually I would do this with a query like { "_id": { "$in": [1, 2] } }
, however having been through the MongoDB docs it appears the aggregation operations are different from regular aggregation operations, and I can't seem to find an aggregate that can achieve this (neither in the docs nor online).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论