Indexes help find data faster in a database. The direction (ascending or descending) tells the database how to sort the data inside the index.
Index direction (ascending vs descending) in MongoDB
db.collection.createIndex({ fieldName: 1 }) // ascending index
db.collection.createIndex({ fieldName: -1 }) // descending indexUse 1 for ascending order and -1 for descending order.
The direction affects sorting and can improve query speed when used correctly.
age field, helping queries that sort or filter by age in ascending order.db.users.createIndex({ age: 1 })orderDate field, helping queries that sort or filter by order date from newest to oldest.db.orders.createIndex({ orderDate: -1 })price ascending and rating descending. Useful for queries sorting by price ascending and rating descending.db.products.createIndex({ price: 1, rating: -1 })This example creates an ascending index on the price field in the products collection. Then it inserts some products and queries them sorted by price from low to high.
use shopDB
db.products.createIndex({ price: 1 })
// Insert sample data
db.products.insertMany([
{ name: "Pen", price: 1.5 },
{ name: "Notebook", price: 3.0 },
{ name: "Pencil", price: 1.0 }
])
// Query sorted by price ascending
db.products.find().sort({ price: 1 }).toArray()Ascending and descending indexes both speed up queries, but choose direction based on how you sort your data.
MongoDB can use an ascending index for descending sorts and vice versa, but specifying direction helps optimize compound indexes.
Creating unnecessary indexes can slow down writes, so only create indexes you need.
Indexes store data in order: ascending (1) or descending (-1).
Choose index direction based on how you sort or filter your queries.
Compound indexes can mix ascending and descending directions for multiple fields.
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
