I have the following types of documents in my mongodb. How can i use a match function to check if the key2 value contains ‘Mermaid / Fairy’ or ‘Superhero’?
{
_id: 123,
key2: [ 'Mermaid / Fairy', 'Superhero' ]
}
{
_id: 456,
key2: [ 'Slug']
}
This is how i am doing matches for individual words, however i would like to pass in a couple, and if it matches any of them, then it gets returned
{
$match: { key2: /.*Superhero.*/ },
},
2
Answers
you can use this aggregate
Here are a couple of ways …
… by checking if the
"$size"
of the"$setIntersection"
of"$key2"
and["Mermaid / Fairy", "Superhero"]
Try it on mongoplayground.net.
Another way is to use
"$reduce"
by checking each"$key2"
value to see if it is"$in"
["Mermaid / Fairy", "Superhero"]
.Try it on mongoplayground.net.