'How to filter aggregation pipeline by specific ids
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
