I have a collection that looks something like this like this (each array element is 1 document):
[
{
"_id": 1,
"title": "title",
"data": [
{
"_id": 41,
"name": "test 1"
},
{
"_id": 42,
"name": "test 2"
},
{
"_id": 43,
"name": "test 3"
}
]
},
{
"_id": 1,
"title": "title 2",
"data": [
{
"_id": 44,
"name": "test 4"
},
{
"_id": 45,
"name": "test 5"
}
]
},
]
What I'd like to do is filter out all documents using $redact
where data._id is $in
an array of [41, 42]
. So, if data._id is ever equal to 41 or 42, the whole document should be kept. Else, it should be pruned.
{
$cond: {
if: {
$in: [
"$data._id",
[ 41, 42 ]
]
},
then: "$$KEEP",
else: "$$PRUNE"
}
}
However, the query above doesn't return anything when I use the $redact
stage in Compass.
I think you are missing the $redact aggregation, apart from that the query looks fine https://www.mongodb.com/docs/manual/reference/operator/aggregation/in/
{
$redact: {
$cond: {
if: {
$in: [
"$data._id",
[ 41, 42 ]
]
},
then: "$$KEEP",
else: "$$PRUNE"
}
}
}