Bird
Raised Fist0
MongoDBquery~20 mins

$all operator for matching all elements in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
MongoDB $all Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents where the 'tags' array contains both 'red' and 'blue'
Given a collection with documents containing a 'tags' array, which query returns documents where 'tags' contains both 'red' and 'blue'?
MongoDB
db.collection.find({ tags: { $all: ['red', 'blue'] } })
Adb.collection.find({ tags: { $all: ['red', 'blue'] } })
Bdb.collection.find({ tags: { $in: ['red', 'blue'] } })
Cdb.collection.find({ tags: { $all: ['red'] } })
Ddb.collection.find({ tags: { $all: ['blue'] } })
Attempts:
2 left
💡 Hint
Use $all to match arrays containing all specified elements.
🧠 Conceptual
intermediate
2:00remaining
Understanding $all operator behavior with nested arrays
What does the $all operator do when matching documents where the field contains nested arrays? For example, if a document has field 'values': [[1,2], [3,4]], what does $all: [[1,2], [3,4]] match?
AMatches documents where 'values' contains any of the numbers 1, 2, 3, or 4
BMatches documents where 'values' contains both arrays [1,2] and [3,4] exactly
CMatches documents where 'values' contains arrays with any of the numbers 1, 2, 3, or 4 in any order
DMatches documents where 'values' contains a single array with all numbers 1, 2, 3, and 4
Attempts:
2 left
💡 Hint
Think about how $all matches elements exactly, including arrays.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this $all query
Which option contains a syntax error in the MongoDB query using $all to match 'colors' containing 'green' and 'yellow'?
MongoDB
db.collection.find({ colors: { $all: 'green', 'yellow' } })
Adb.collection.find({ colors: { $all: ['green', 'yellow'] } })
Bdb.collection.find({ colors: { $all: ['yellow'] } })
Cdb.collection.find({ colors: { $all: ['green'] } })
Ddb.collection.find({ colors: { $all: 'green', 'yellow' } })
Attempts:
2 left
💡 Hint
Check the syntax for $all operator's value type.
optimization
advanced
2:00remaining
Optimizing a query using $all with multiple elements
You want to find documents where the 'features' array contains 'wifi', 'parking', and 'pool'. Which query is the most efficient and correct?
Adb.collection.find({ $and: [ { features: 'wifi' }, { features: 'parking' }, { features: 'pool' } ] })
Bdb.collection.find({ features: { $in: ['wifi', 'parking', 'pool'] } })
Cdb.collection.find({ features: { $all: ['wifi', 'parking', 'pool'] } })
Ddb.collection.find({ features: 'wifi' }).find({ features: 'parking' }).find({ features: 'pool' })
Attempts:
2 left
💡 Hint
Use a single query operator that matches all elements at once.
🔧 Debug
expert
3:00remaining
Why does this $all query return no results?
You run this query: db.collection.find({ tags: { $all: ['apple', 'banana'] } }) but get no results, even though documents have 'tags' arrays with 'apple' and 'banana' but in nested arrays like ['apple', ['banana']] or [['apple'], 'banana']. Why?
ABecause $all matches exact elements, nested arrays are not matched as individual elements
BBecause $all only matches strings, not arrays
CBecause the query syntax is incorrect and causes no matches
DBecause $all requires the elements to be in order in the array
Attempts:
2 left
💡 Hint
Consider how $all matches elements exactly, including nested arrays.

Practice

(1/5)
1.

What does the $all operator do in MongoDB queries?

easy
A. Matches documents where an array contains any one of the specified values.
B. Matches documents where an array contains all specified values.
C. Matches documents where an array contains exactly one specified value.
D. Matches documents where an array is empty.

Solution

  1. Step 1: Understand the purpose of $all

    The $all operator is used to find documents where an array field contains all the values specified in the query.
  2. Step 2: Compare with other operators

    Unlike $in which matches any value, $all requires all values to be present in the array.
  3. Final Answer:

    Matches documents where an array contains all specified values. -> Option B
  4. Quick Check:

    $all = all values present [OK]
Hint: Remember: $all means every value must be in the array [OK]
Common Mistakes:
  • Confusing $all with $in operator
  • Thinking $all checks order of elements
  • Assuming $all matches partial values
2.

Which of the following is the correct syntax to find documents where the tags array contains both "red" and "blue" using $all?

{ tags: { $all: ["red", "blue"] } }
easy
A. { tags: { $all: ["red", "blue"] } }
B. { tags: { $all: "red", "blue" } }
C. { tags: { $all: "red blue" } }
D. { tags: { $all: { "red", "blue" } } }

Solution

  1. Step 1: Check the correct structure for $all

    The $all operator requires an array of values to match all elements.
  2. Step 2: Validate each option's syntax

    { tags: { $all: ["red", "blue"] } } correctly uses an array with square brackets. Options A, B, and D use incorrect syntax for arrays or objects.
  3. Final Answer:

    { tags: { $all: ["red", "blue"] } } -> Option A
  4. Quick Check:

    Correct array syntax for $all [OK]
Hint: Use square brackets [] to list values inside $all [OK]
Common Mistakes:
  • Using curly braces {} instead of square brackets []
  • Passing values as separate arguments instead of an array
  • Using a string instead of an array for $all
3.

Given the collection documents:

[{ "colors": ["red", "green", "blue"] }, { "colors": ["red", "yellow"] }, { "colors": ["blue", "green", "red"] }]

What will the query { colors: { $all: ["red", "blue"] } } return?

medium
A. No documents
B. Document 2 only
C. Documents 1 and 3
D. All three documents

Solution

  1. Step 1: Check each document's colors array

    Document 1 has ["red", "green", "blue"] which includes both "red" and "blue". Document 2 has ["red", "yellow"] missing "blue". Document 3 has ["blue", "green", "red"] which includes both.
  2. Step 2: Apply $all condition

    The query matches documents where both "red" and "blue" are present, so documents 1 and 3 match.
  3. Final Answer:

    Documents 1 and 3 -> Option C
  4. Quick Check:

    Both arrays contain "red" and "blue" [OK]
Hint: Check each array contains all values, order doesn't matter [OK]
Common Mistakes:
  • Assuming order matters for $all
  • Including documents missing one value
  • Confusing $all with $in behavior
4.

Identify the error in this query that tries to find documents where features array contains both "wifi" and "parking":

{ features: { $all: "wifi", "parking" } }
medium
A. The field name should be inside quotes.
B. The values should be inside curly braces instead of quotes.
C. The query should use $in instead of $all.
D. The $all operator requires an array of values, not separate arguments.

Solution

  1. Step 1: Analyze the $all operator usage

    The $all operator expects a single array containing all values to match.
  2. Step 2: Identify the syntax error

    The query incorrectly passes two separate string arguments instead of an array. It should be { $all: ["wifi", "parking"] }.
  3. Final Answer:

    The $all operator requires an array of values, not separate arguments. -> Option D
  4. Quick Check:

    $all needs an array [OK]
Hint: Always wrap $all values in an array [] [OK]
Common Mistakes:
  • Passing multiple values without array brackets
  • Using $in when $all is needed
  • Misplacing quotes around field names
5.

You have a collection of documents with a field ingredients which is an array of strings. You want to find all recipes that contain both "flour" and "sugar", but not "nuts". Which query correctly uses $all and other operators to achieve this?

hard
A. { ingredients: { $all: ["flour", "sugar"], $nin: ["nuts"] } }
B. { ingredients: { $all: ["flour", "sugar", "nuts"] } }
C. { ingredients: { $in: ["flour", "sugar"], $nin: ["nuts"] } }
D. { ingredients: { $all: ["flour", "sugar"] }, ingredients: { $nin: ["nuts"] } }

Solution

  1. Step 1: Use $all to match both "flour" and "sugar"

    The $all operator ensures the array contains both these ingredients.
  2. Step 2: Use $nin to exclude "nuts"

    The $nin operator excludes documents where the array contains "nuts".
  3. Step 3: Combine both conditions correctly

    { ingredients: { $all: ["flour", "sugar"], $nin: ["nuts"] } } combines $all and $nin inside the same field query, which is valid MongoDB syntax.
  4. Final Answer:

    { ingredients: { $all: ["flour", "sugar"], $nin: ["nuts"] } } -> Option A
  5. Quick Check:

    All required and no excluded ingredients [OK]
Hint: Combine $all and $nin inside one field object [OK]
Common Mistakes:
  • Putting $all and $nin in separate objects for same field
  • Using $in instead of $all for required ingredients
  • Including excluded items inside $all array