Skip to content
Advertisement

mongodb – match if a group of values from an array is in another given array [with Aggregation]

I’m looking for a solution since 3 hours and I don’t get it yet.

I have the following collection:

JavaScript

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:

JavaScript

[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:

JavaScript

Example here

  • Using $elemMatch and double negation ($not and $nin):
JavaScript

Example here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement