I’m looking for a solution since 3 hours and I don’t get it yet.
I have the following collection:
{text: 'random text 1', indexes:[1,2] }, {text: 'random text 2', indexes:[1,3] }, {text: 'random text 3', indexes:[2,4] },
and I would like to have only the documents that have all the index values in a given array like for example [1,2,4]
using the example above, I would like to have the following output:
{text: 'random text 1', indexes:[1,2] }, {text: 'random text 3', indexes:[2,4] },
[1,2]
is in [1,2,4]
-> OK
[1,3]
is not in [1,2,4]
because of 3 -> Not OK
[1,4]
is in [1,2,4]
-> OK
Any idea? thanks for your answer! 🙂
Advertisement
Answer
You can use one of this this $match
stage:
- Using $setIsSubset into
$expr
:
{ "$match": { "$expr": { "$setIsSubset": ["$indexes",[1,2,4]] } } }
Example here
- Using
$elemMatch
and double negation ($not
and$nin
):
{ "$match": { "indexes": { "$not": { "$elemMatch": { "$nin": [1,2,4] } } } } }
Example here