Combining logical and comparison operators in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we combine logical and comparison operators in MongoDB queries, we want to know how the time to run the query changes as the data grows.
We ask: How does the query speed change when there are more documents to check?
Analyze the time complexity of the following code snippet.
db.collection.find({
$and: [
{ age: { $gt: 25 } },
{ $or: [
{ status: "A" },
{ score: { $lt: 50 } }
]}
]
})
This query finds documents where the age is greater than 25 and either the status is "A" or the score is less than 50.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each document in the collection against the combined conditions.
- How many times: Once per document, the query engine tests the age, status, and score fields.
As the number of documents grows, the query must check more documents to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks of age, status, and score |
| 100 | About 100 checks of age, status, and score |
| 1000 | About 1000 checks of age, status, and score |
Pattern observation: The number of operations grows directly with the number of documents.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of documents increases.
[X] Wrong: "Using multiple conditions with $and and $or makes the query slower than checking each condition separately."
[OK] Correct: MongoDB checks all conditions together in one pass per document, so combining them does not multiply the work but keeps it proportional to the number of documents.
Understanding how combined logical and comparison operators affect query time helps you explain how databases handle complex filters efficiently.
"What if we added an index on the age field? How would the time complexity change?"
Practice
Solution
Step 1: Understand logical operators
$and requires all conditions inside it to be true for a document to match.Step 2: Compare with other operators
$or requires any condition to be true, $gt and $lt are comparison operators, not logical combiners.Final Answer:
$and -> Option BQuick Check:
All conditions true = $and [OK]
- Confusing $and with $or
- Using comparison operators as logical
- Mixing $gt with $and
Solution
Step 1: Check $and syntax
$and requires an array of conditions, so the value must be an array of objects.Step 2: Validate other options
{ $or: [ { age: { $gt: 25 } }, { status: 'active' } ] } uses $or, { age: { $gt: 25 }, status: 'active' } uses implicit AND but not with $and operator, { $and: { age: { $gt: 25 }, status: 'active' } } uses $and with an object instead of array which is invalid.Final Answer:
{ $and: [ { age: { $gt: 25 } }, { status: 'active' } ] } -> Option AQuick Check:
$and needs array of conditions [OK]
- Using object instead of array in $and
- Confusing $and with $or
- Missing $gt operator syntax
{ $or: [ { age: { $lt: 25 } }, { status: 'active' } ] }Solution
Step 1: Understand $or condition
Documents match if age is less than 25 OR status is 'active'.Step 2: Check each document
First doc: age 30 (not <25), status 'active' (matches). Second doc: age 20 (<25), status 'inactive' (matches age condition). Third doc: age 40 (not <25), status 'active' (matches).Final Answer:
All three documents match -> Option DQuick Check:
$or matches any condition true [OK]
- Ignoring one condition in $or
- Confusing $or with $and
- Wrongly excluding documents
{ $and: { score: { $gt: 50 }, score: { $lt: 100 } } }Solution
Step 1: Check $and operator usage
$and requires an array of condition objects, not a single object with repeated keys.Step 2: Understand object key overwrite
In the object, 'score' key appears twice, so only the last one is used, causing incorrect query.Final Answer:
Using object instead of array inside $and -> Option AQuick Check:
$and needs array, not object with duplicate keys [OK]
- Using object with duplicate keys in $and
- Confusing $and with $or
- Thinking $gt and $lt can't combine
Solution
Step 1: Break down the condition
We want documents matching either (age > 30 AND status = 'active') OR (score < 50).Step 2: Match query structure
{ $or: [ { $and: [ { age: { $gt: 30 } }, { status: 'active' } ] }, { score: { $lt: 50 } } ] } uses $or with two parts: one $and array for age and status, and one condition for score less than 50, matching the requirement exactly.Final Answer:
{ $or: [ { $and: [ { age: { $gt: 30 } }, { status: 'active' } ] }, { score: { $lt: 50 } } ] } -> Option CQuick Check:
Nested $and inside $or matches complex logic [OK]
- Using $and instead of $or for main condition
- Wrong object instead of array syntax
- Mixing $or and $and incorrectly
