Discover how a simple choice in sorting direction can speed up your data searches dramatically!
Index direction (ascending vs descending) in MongoDB - When to Use Which
Imagine you have a huge phone book sorted by last names. You want to find all people whose last names start with 'M'. Without any order, you'd have to flip through every page one by one.
Searching manually through an unordered list is slow and tiring. You might miss entries or waste time flipping back and forth. It's easy to get lost or make mistakes when the data isn't organized clearly.
Using index direction, like ascending or descending order, organizes data so you can quickly jump to the right spot. It's like having the phone book sorted from A to Z or Z to A, making searches fast and reliable.
db.contacts.find({lastName: {$gte: 'M', $lt: 'N'}})db.contacts.createIndex({lastName: 1}) // ascending indexIt lets databases find and sort data lightning-fast, improving user experience and saving time.
When you scroll through a social media feed sorted by newest posts first (descending), the index direction helps load those posts instantly without delay.
Manual searching through unordered data is slow and error-prone.
Index direction organizes data ascending or descending for quick access.
This makes searching and sorting efficient and reliable.
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
