How do you filter documents where an array contains a specific value?
To filter documents where an array contains a specific value, use an ordinary comparison against the array field.
Example:
js
db.users.find({ skills: "MongoDB" })If a document has an array:
json
{ "name": "Alex", "skills": ["MongoDB", "Node.js"] }- that document will be found, because
"MongoDB"is one of the elements ofskills.
So: to filter by an array, it's enough to name the field and the value, MongoDB checks on its own whether the array contains that element.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.