Problem Statement
Which operator matches documents where an array field contains all specified elements?
Explanation
The dollar all operator matches documents where an array field contains all the specified elements, regardless of order or additional elements. This is useful when you need to ensure an array has multiple specific values.
For example, if you want to find users who have both JavaScript and Python skills, you use dollar all. The dollar in operator, in contrast, matches documents where a field equals any value in the specified array. The dollar elemMatch is used for complex array queries with multiple conditions on array elements.
Code Solution
SolutionRead Only
// Find users with both JavaScript and Python skills
db.users.find({
skills: { $all: ["JavaScript", "Python"] }
})
// This matches:
{ skills: ["JavaScript", "Python", "Java"] }
{ skills: ["Python", "JavaScript"] }
// But not:
{ skills: ["JavaScript", "Java"] }