0
0
MongoDBquery~10 mins

Index direction (ascending vs descending) in MongoDB - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Index direction (ascending vs descending)
Create Index
Specify Field and Direction
Ascending (1) or Descending (-1)?
Ascending: Sorts from smallest to largest
Descending: Sorts from largest to smallest
Index Built with Direction
Query Uses Index for Sorting
When creating an index in MongoDB, you specify the field and direction (1 for ascending, -1 for descending). This direction controls how the index sorts the data, which affects query performance when sorting.
Execution Sample
MongoDB
db.collection.createIndex({ age: 1 })
db.collection.createIndex({ age: -1 })
Creates two indexes on the 'age' field: one ascending and one descending.
Execution Table
StepActionFieldDirectionIndex StateEffect on Query
1Create indexage1 (ascending)Index on age ascending createdQueries sorting age ascending use this index efficiently
2Create indexage-1 (descending)Index on age descending createdQueries sorting age descending use this index efficiently
3Query with sort age ascendingage1Uses ascending indexFast sort from smallest to largest age
4Query with sort age descendingage-1Uses descending indexFast sort from largest to smallest age
5Query with sort age ascending but only descending index existsage1No matching ascending indexQuery slower, may need in-memory sort
💡 Index direction determines which queries can use the index for sorting; if direction mismatches, index may not be used.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Index on ageNoneAscending (1)Ascending (1) + Descending (-1)Ascending (1) + Descending (-1)Ascending (1) + Descending (-1)Ascending (1) + Descending (-1)
Key Moments - 2 Insights
Why do we specify 1 or -1 when creating an index?
1 means ascending order (smallest to largest), -1 means descending order (largest to smallest). This affects how MongoDB sorts data using the index, as shown in execution_table rows 1 and 2.
Can a descending index be used for ascending sort queries?
No, MongoDB uses the index direction to optimize sorting. If the query sort direction doesn't match the index direction, the index won't be used for sorting efficiently (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which step shows the creation of a descending index?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Direction' column for '-1 (descending)' in the execution_table.
At which step does a query use the ascending index for sorting?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Look for 'Uses ascending index' in the 'Index State' column in the execution_table.
If only an ascending index exists, what happens when a query sorts descending?
AUses ascending index efficiently
BUses descending index efficiently
CNo matching index, slower query
DQuery fails
💡 Hint
See execution_table row 5 about query with sort direction mismatch.
Concept Snapshot
MongoDB indexes can be ascending (1) or descending (-1).
Direction controls how data is sorted in the index.
Queries sorting in the same direction use the index efficiently.
If directions mismatch, index may not be used for sorting.
Specify direction when creating index: { field: 1 } or { field: -1 }.
Full Transcript
In MongoDB, when you create an index, you specify the field and the direction: 1 for ascending or -1 for descending. This direction tells MongoDB how to sort the data in the index. For example, an ascending index sorts from smallest to largest values, while a descending index sorts from largest to smallest. When you run queries that sort data, MongoDB uses the index that matches the sort direction to speed up the query. If the query's sort direction does not match any index direction, MongoDB cannot use the index for sorting efficiently, which may slow down the query. This visual trace showed creating ascending and descending indexes on the 'age' field, and how queries use these indexes depending on their sort direction.