'Find document in which array match any items in query array | mongodb

I have Schema

person = {
 skill = [{
        type: String
    }]
 name = { type : String}
}

I have a skill array skill = ['python', 'css']

I want all the people that match at least one skill from the skill array.

$all and $in retrieve only people that match all the skills in skill array but I want skill people that match at least skill from skill array.



Solution 1:[1]

You can use "$in" for your purpose. Perhaps you had some other issue when you tried it before.

db.collection.find({
  "skill": {
    "$in": [ "python", "css" ]
  }
})

Try it on mongoplayground.net.

Just for fun, here's another mongoplayground.net example that uses a mgodatagen configuration to generate the collection. The query is the same.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1