Query filter syntax in MongoDB - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use query filters in MongoDB, we want to know how the time to find data changes as the data grows.
We ask: How does the filter affect the work MongoDB does as the collection gets bigger?
Analyze the time complexity of the following code snippet.
db.users.find({ "age": { "$gt": 25 } })
This code finds all users older than 25 years in the users collection.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning documents to check if the age is greater than 25.
- How many times: Once for each document in the collection until all are checked or results found.
As the number of users grows, MongoDB checks more documents to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of documents.
Time Complexity: O(n)
This means the time to find matching documents grows linearly as the collection size grows.
[X] Wrong: "Using a filter always makes the query very fast regardless of data size."
[OK] Correct: Without an index, MongoDB must check each document, so the time grows with data size.
Understanding how filters affect query time helps you explain how databases handle searches as data grows, a useful skill in many real projects.
"What if we added an index on the age field? How would the time complexity change?"
Practice
{ age: 25 }Solution
Step 1: Understand the role of a query filter
A query filter in MongoDB is used to find documents that match certain conditions, not to modify or delete them.Step 2: Analyze the filter
This filter matches documents where the field{ age: 25 }agehas the value 25 exactly.Final Answer:
Selects documents where the age field is exactly 25 -> Option CQuick Check:
Query filter = select matching documents [OK]
- Confusing filter with update or delete commands
- Thinking filter creates or changes documents
- Assuming filter matches partial or range values without operators
score is greater than 80?Solution
Step 1: Recall MongoDB operator syntax
MongoDB uses JSON objects with operators starting with a dollar sign, like$gtfor 'greater than'.Step 2: Check each option
{ score: { $gt: 80 } } uses{ score: { $gt: 80 } }which is correct syntax. Others miss the dollar sign or use invalid syntax.Final Answer:
{ score: { $gt: 80 } } -> Option DQuick Check:
Use $gt for greater than in filters [OK]
- Omitting $ before operator
- Using comparison operators like > directly
- Wrong operator names without $
{ name: "Alice", age: 30 }
{ name: "Bob", age: 25 }
{ name: "Carol", age: 30 }What will this query return?
db.collection.find({ age: 30 })Solution
Step 1: Understand the filter condition
The filter{ age: 30 }matches documents where the age field equals 30.Step 2: Identify matching documents
Documents for Alice and Carol have age 30, so both match. Bob has age 25, so does not match.Final Answer:
[{ name: "Alice", age: 30 }, { name: "Carol", age: 30 }] -> Option AQuick Check:
Filter matches exact age 30 documents [OK]
- Expecting only one document returned
- Confusing filter with update or delete
- Thinking filter matches partial or range without operators
db.collection.find({ age: $gt: 20 })Solution
Step 1: Check operator syntax in filter
MongoDB requires operators like$gtto be inside an object as a value for the field key.Step 2: Identify the syntax error
The query uses{ age: $gt: 20 }which is invalid. It should be{ age: { $gt: 20 } }with curly braces around$gt: 20.Final Answer:
Missing curly braces around $gt operator -> Option BQuick Check:
Operators must be inside braces as field values [OK]
- Omitting braces around operator
- Using wrong operator names
- Misplacing $ sign
status is either "active" or "pending". Which filter correctly does this?Solution
Step 1: Understand how to match multiple values
MongoDB uses$inoperator to match a field against any value in a list.Step 2: Analyze each option
{ status: { $in: ["active", "pending"] } } uses{ status: { $in: ["active", "pending"] } }which is correct. { status: { $or: ["active", "pending"] } } uses invalid operator$orinside field. { $or: { status: "active", status: "pending" } } has wrong syntax for$or. { status: "active" || "pending" } uses invalid JavaScript syntax.Final Answer:
{ status: { $in: ["active", "pending"] } } -> Option AQuick Check:
Use $in for matching any value in list [OK]
- Using $or inside field instead of top-level
- Wrong syntax for $or operator
- Using JavaScript operators inside filter
