$all operator for matching all elements in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to run a MongoDB query using the $all operator changes as the data grows.
Specifically, how does checking if a document's array contains all specified elements affect performance?
Analyze the time complexity of the following code snippet.
db.collection.find({
tags: { $all: ["red", "blue", "green"] }
})
This query finds documents where the tags array contains all three colors: red, blue, and green.
Look at what repeats when MongoDB checks each document.
- Primary operation: Checking each document's
tagsarray to see if it contains all specified elements. - How many times: This check happens once per document in the collection or index scan.
As the number of documents grows, MongoDB must check more arrays.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 array checks |
| 100 | About 100 array checks |
| 1000 | About 1000 array checks |
Pattern observation: The number of checks grows directly with the number of documents.
Time Complexity: O(n * m)
This means the time to run the query grows linearly with the number of documents and the number of elements to check in each array.
[X] Wrong: "Using $all is always fast because it just checks a few elements."
[OK] Correct: MongoDB must check each document's array, so if there are many documents, it takes longer.
Understanding how queries like $all scale helps you write better database queries and explain your choices clearly.
"What if we added an index on the tags field? How would that change the time complexity?"
Practice
What does the $all operator do in MongoDB queries?
Solution
Step 1: Understand the purpose of
The$all$alloperator is used to find documents where an array field contains all the values specified in the query.Step 2: Compare with other operators
Unlike$inwhich matches any value,$allrequires all values to be present in the array.Final Answer:
Matches documents where an array contains all specified values. -> Option BQuick Check:
$all= all values present [OK]
- Confusing $all with $in operator
- Thinking $all checks order of elements
- Assuming $all matches partial values
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"] } }Solution
Step 1: Check the correct structure for $all
The$alloperator requires an array of values to match all elements.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.Final Answer:
{ tags: { $all: ["red", "blue"] } } -> Option AQuick Check:
Correct array syntax for $all [OK]
- 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
Given the collection documents:
[{ "colors": ["red", "green", "blue"] }, { "colors": ["red", "yellow"] }, { "colors": ["blue", "green", "red"] }]What will the query { colors: { $all: ["red", "blue"] } } return?
Solution
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.Step 2: Apply $all condition
The query matches documents where both "red" and "blue" are present, so documents 1 and 3 match.Final Answer:
Documents 1 and 3 -> Option CQuick Check:
Both arrays contain "red" and "blue" [OK]
- Assuming order matters for $all
- Including documents missing one value
- Confusing $all with $in behavior
Identify the error in this query that tries to find documents where features array contains both "wifi" and "parking":
{ features: { $all: "wifi", "parking" } }Solution
Step 1: Analyze the $all operator usage
The $all operator expects a single array containing all values to match.Step 2: Identify the syntax error
The query incorrectly passes two separate string arguments instead of an array. It should be{ $all: ["wifi", "parking"] }.Final Answer:
The $all operator requires an array of values, not separate arguments. -> Option DQuick Check:
$all needs an array [OK]
- Passing multiple values without array brackets
- Using $in when $all is needed
- Misplacing quotes around field names
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?
Solution
Step 1: Use $all to match both "flour" and "sugar"
The $all operator ensures the array contains both these ingredients.Step 2: Use $nin to exclude "nuts"
The $nin operator excludes documents where the array contains "nuts".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.Final Answer:
{ ingredients: { $all: ["flour", "sugar"], $nin: ["nuts"] } } -> Option AQuick Check:
All required and no excluded ingredients [OK]
- Putting $all and $nin in separate objects for same field
- Using $in instead of $all for required ingredients
- Including excluded items inside $all array
