Index direction (ascending vs descending) in MongoDB - Performance Comparison
When using indexes in MongoDB, the direction of the index (ascending or descending) can affect how quickly queries run.
We want to understand if changing the index direction changes how the query time grows as data grows.
Analyze the time complexity of queries using ascending and descending indexes.
// Create ascending index
db.collection.createIndex({ age: 1 })
// Create descending index
db.collection.createIndex({ age: -1 })
// Query using ascending index
db.collection.find({ age: { $gte: 30 } }).sort({ age: 1 })
// Query using descending index
db.collection.find({ age: { $gte: 30 } }).sort({ age: -1 })
This code creates two indexes on the same field but with different directions, then runs queries that use those indexes.
Look at what repeats when the query runs:
- Primary operation: Scanning index entries matching the query condition.
- How many times: Once per matching document in the index range.
As the number of matching documents grows, the number of index entries scanned grows roughly the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 index entries scanned |
| 100 | About 100 index entries scanned |
| 1000 | About 1000 index entries scanned |
Pattern observation: The number of operations grows linearly with the number of matching documents, regardless of index direction.
Time Complexity: O(n)
This means the query time grows in a straight line with the number of matching documents, whether the index is ascending or descending.
[X] Wrong: "Using descending indexes is slower than ascending indexes because the order is reversed."
[OK] Correct: MongoDB treats ascending and descending indexes the same way internally, so the direction does not affect how fast the query runs.
Understanding that index direction does not change time complexity helps you focus on what really matters: choosing the right fields to index for your queries.
What if we added a compound index with mixed directions? How would that affect the time complexity of queries using that index?
Practice
1 mean in MongoDB indexes?Solution
Step 1: Understand index direction values
In MongoDB, index direction1means ascending order, and-1means descending order.Step 2: Match the value to the meaning
Since the question asks about1, it corresponds to ascending order.Final Answer:
Ascending order -> Option CQuick Check:
Index direction 1 = Ascending order [OK]
- Confusing 1 with descending order
- Thinking 1 means no order
- Mixing up ascending and descending
age in MongoDB?Solution
Step 1: Recall MongoDB index syntax for direction
MongoDB uses1for ascending and-1for descending in index creation.Step 2: Identify the correct descending syntax
The correct way to create a descending index onageis{ age: -1 }.Final Answer:
db.collection.createIndex({ age: -1 }) -> Option BQuick Check:
Descending index uses -1 [OK]
- Using string values like 'desc' instead of -1
- Using 1 which means ascending
- Incorrect object syntax
{ name: 1, score: -1 }, which query will use this index efficiently?Solution
Step 1: Understand compound index direction
The index sortsnameascending andscoredescending.Step 2: Match query sort order with index
Only the query sorting{ name: 1, score: -1 }matches the index directions exactly, so it uses the index efficiently.Final Answer:
db.collection.find().sort({ name: 1, score: -1 }) -> Option AQuick Check:
Sort matches index directions exactly [OK]
- Reversing sort directions
- Changing field order in sort
- Using partial sort that doesn't match index
{ date: 1, price: -1 } but your query sorts by { price: -1, date: 1 }. What is the problem?Solution
Step 1: Check index field order
The index is created with fieldsdatethenprice.Step 2: Compare query sort order
The query sorts bypricefirst, thendate, which does not match the index field order.Final Answer:
The index fields order does not match the sort order -> Option AQuick Check:
Field order in sort must match index order [OK]
- Assuming direction values are invalid
- Thinking MongoDB disallows mixed directions
- Blaming query syntax instead of order
category ascending and then by rating descending. Which index should you create?Solution
Step 1: Identify query sort order
The queries sort bycategoryascending andratingdescending.Step 2: Create matching compound index
The index must havecategorywith direction1andratingwith direction-1in that order.Final Answer:
db.collection.createIndex({ category: 1, rating: -1 }) -> Option DQuick Check:
Index matches query sort directions and order [OK]
- Reversing field order in index
- Swapping ascending and descending directions
- Creating index on wrong fields
Ready to prove it?
Pass the test and this pattern moves unknown
