How MongoDB scans documents - Performance & Efficiency
Start learning this pattern below
Jump into concepts and practice - no test required
When MongoDB looks for data, it scans documents to find matches. Understanding how long this takes helps us know how fast queries run.
We want to see how the time to scan grows as the number of documents grows.
Analyze the time complexity of the following code snippet.
// Find all documents where age is 25
db.users.find({ age: 25 })
This query scans the users collection to find documents with age equal to 25.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each document in the collection to check the age field.
- How many times: Once for every document in the collection.
As the number of documents grows, MongoDB checks more documents one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 document checks |
| 100 | 100 document checks |
| 1000 | 1000 document checks |
Pattern observation: The number of checks grows directly with the number of documents.
Time Complexity: O(n)
This means the time to find matching documents grows in a straight line as the collection gets bigger.
[X] Wrong: "MongoDB instantly finds matching documents no matter the collection size."
[OK] Correct: Without indexes, MongoDB must look at each document one by one, so bigger collections take more time.
Knowing how MongoDB scans documents helps you explain query speed and shows you understand how databases work behind the scenes.
"What if we added an index on the age field? How would the time complexity change?"
Practice
Solution
Step 1: Understand MongoDB scanning without indexes
Without indexes, MongoDB must check each document to find matches.Step 2: Recognize the scanning method
This means MongoDB performs a full collection scan, checking documents one by one.Final Answer:
It scans every document one by one. -> Option DQuick Check:
No index means full scan = It scans every document one by one. [OK]
- Thinking MongoDB returns an error without indexes
- Assuming MongoDB uses cached results automatically
- Believing MongoDB scans only part of the collection
age in MongoDB?Solution
Step 1: Recall MongoDB index creation syntax
The correct syntax usescreateIndexwith a document specifying field and order.Step 2: Match syntax to options
db.collection.createIndex({age: 1}) uses{age: 1}which means ascending order, the correct format.Final Answer:
db.collection.createIndex({age: 1}) -> Option AQuick Check:
Index creation uses createIndex({field: order}) = db.collection.createIndex({age: 1}) [OK]
- Using a string instead of an object for fields
- Using incorrect method names like create or index
- Passing field name without order direction
age, what documents will MongoDB scan for the query {age: {$gt: 28}}?Solution
Step 1: Understand query and index usage
The query asks for documents where age is greater than 28. The index on age helps MongoDB find matching documents efficiently.Step 2: Identify matching documents
Documents with age 30 and 35 satisfy the condition, so MongoDB scans only these two.Final Answer:
Only documents with age 30 and 35 -> Option AQuick Check:
Index filters to matching docs = Only documents with age 30 and 35 [OK]
- Thinking index scans no documents at all
- Assuming all documents are scanned despite index
- Selecting only one matching document incorrectly
db.users.find({age: {$lt: 20}}) but it scans all documents even though you created an index on age. What is the likely problem?Solution
Step 1: Check index field correctness
If the index is not on theagefield, MongoDB cannot use it for this query.Step 2: Confirm MongoDB capabilities
MongoDB supports indexes on numeric fields and range queries ($lt, $gt, etc.). The provided query syntax is correct.Final Answer:
The index was created on a different field, not age. -> Option BQuick Check:
Index field mismatch causes full scan = The index was created on a different field, not age. [OK]
- Believing MongoDB can't index numeric fields
- Assuming range queries never use indexes
- Thinking query syntax is invalid when it is correct
status. You run db.collection.find({status: 'active', score: {$gt: 50}}). MongoDB scans many documents even though status is indexed. Why?Solution
Step 1: Analyze query with multiple conditions
The query filters onstatusandscore. Onlystatusis indexed.Step 2: Understand index usage with multiple fields
MongoDB uses the index onstatusto find matching documents, but must scan those documents to checkscorebecause it is not indexed.Final Answer:
Becausescoreis not indexed, MongoDB scans documents matchingstatusto checkscorecondition. -> Option CQuick Check:
Partial index use requires scanning for other conditions = Becausescoreis not indexed, MongoDB scans documents matchingstatusto checkscorecondition. [OK]
- Thinking MongoDB can't use indexes with multiple conditions
- Assuming query syntax is wrong when it is correct
- Believing indexes speed up all conditions automatically
