Identifying missing indexes in MongoDB - Time & Space Complexity
When we look for missing indexes in MongoDB, we want to understand how the time to find data grows as the data grows.
We ask: How does the search time change if we don't have the right index?
Analyze the time complexity of the following MongoDB query without an index.
db.orders.find({ customerId: 12345 })
This query searches for all orders from a specific customer in the orders collection.
Look at what repeats when the query runs without an index.
- Primary operation: Scanning each document in the collection one by one.
- How many times: Once for every document in the collection.
As the number of documents grows, the time to find matching orders grows too.
| 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 search time grows linearly with the number of documents when no index is used.
[X] Wrong: "The database will find the data instantly even without indexes."
[OK] Correct: Without indexes, the database must look at every document, so it takes longer as data grows.
Understanding how missing indexes affect query time helps you explain why indexes matter in real projects.
"What if we add an index on customerId? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of indexes
Indexes are special data structures that help MongoDB quickly locate data without scanning the entire collection.Step 2: Compare options with index purpose
Only To make queries run faster by helping MongoDB find data quickly correctly states that indexes speed up queries by helping find data faster.Final Answer:
To make queries run faster by helping MongoDB find data quickly -> Option BQuick Check:
Indexes improve query speed = A [OK]
- Thinking indexes store more data
- Confusing indexes with backups
- Assuming indexes encrypt data
users?Solution
Step 1: Recall the correct method to list indexes
The correct MongoDB method to list indexes on a collection islistIndexes().Step 2: Check each option's validity
Onlydb.users.listIndexes()is a valid MongoDB command; others are incorrect or do not exist.Final Answer:
db.users.listIndexes() -> Option DQuick Check:
List indexes command = listIndexes() [OK]
- Using getIndex() which is not a MongoDB method
- Trying findIndexes() which does not exist
- Confusing showIndexes() with listIndexes()
db.orders.find({ customerId: 123 }) and the collection has no index on customerId, what will explain('executionStats') likely show?Solution
Step 1: Understand query behavior without index
Without an index oncustomerId, MongoDB must scan the entire collection (COLLSCAN) to find matching documents.Step 2: Interpret explain output
Theexplain('executionStats')will show a COLLSCAN and a high number of documents examined, indicating no index was used.Final Answer:
A COLLSCAN (collection scan) with high number of documents examined -> Option AQuick Check:
No index means COLLSCAN = B [OK]
- Expecting an index scan without an index
- Thinking explain causes syntax error
- Assuming no documents returned without index
db.products.find({ category: 'books', price: { $lt: 20 } }). The collection has an index only on category. What is the best fix?Solution
Step 1: Analyze existing index and query filters
The query filters on bothcategoryandprice, but onlycategoryis indexed, so the price filter is not optimized.Step 2: Choose the best index to optimize query
A compound index on{ category: 1, price: 1 }allows MongoDB to efficiently filter by both fields, speeding up the query.Final Answer:
Add a compound index on { category: 1, price: 1 } -> Option CQuick Check:
Compound index matches query fields = D [OK]
- Removing useful indexes
- Adding index on only one field when query filters multiple
- Changing query to remove filters instead of indexing
logs has slow queries filtering by userId and sorting by timestamp. Which index best improves performance?Solution
Step 1: Understand query pattern with filter and sort
The query filters byuserIdand sorts bytimestamp, so both fields affect performance.Step 2: Select index that supports filter and sort
A compound index on{ userId: 1, timestamp: -1 }supports efficient filtering byuserIdand sorting bytimestampin descending order.Final Answer:
A compound index on { userId: 1, timestamp: -1 } -> Option AQuick Check:
Compound index matches filter and sort = C [OK]
- Using single-field index ignoring sort
- Choosing wrong sort order in index
- Ignoring indexing and blaming hardware
Ready to prove it?
Pass the test and this pattern moves unknown
