Create and Use Ascending and Descending Indexes in MongoDB
📖 Scenario: You are managing a small online bookstore database. You want to speed up searches on book titles and prices by creating indexes. You will create one index that sorts titles in ascending order and another index that sorts prices in descending order.
🎯 Goal: Build MongoDB indexes on the books collection: one index on title in ascending order and another index on price in descending order.
📋 What You'll Learn
Create a books collection with three documents containing title and price fields.
Create a variable called titleIndex that defines an ascending index on the title field.
Create a variable called priceIndex that defines a descending index on the price field.
Create the ascending index on title using db.books.createIndex() with titleIndex.
Create the descending index on price using db.books.createIndex() with priceIndex.
💡 Why This Matters
🌍 Real World
Indexes are used in real databases to speed up searches and sorting, just like an index in a book helps you find pages quickly.
💼 Career
Database administrators and developers create and manage indexes to optimize application performance and handle large data efficiently.
Progress0 / 4 steps
1
DATA SETUP: Create the books collection with sample documents
Insert three documents into the books collection with these exact entries: { title: "The Hobbit", price: 15 }, { title: "1984", price: 12 }, and { title: "Brave New World", price: 18 }.
MongoDB
Hint
Use db.books.insertMany() with an array of objects for the books.
2
CONFIGURATION: Define index variables for ascending and descending order
Create a variable called titleIndex that sets title to 1 for ascending order, and a variable called priceIndex that sets price to -1 for descending order.
MongoDB
Hint
Use const to create the variables with the correct field and direction values.
3
CORE LOGIC: Create the ascending index on title
Use db.books.createIndex() with the variable titleIndex to create an ascending index on the title field.
MongoDB
Hint
Call db.books.createIndex() with titleIndex as the argument.
4
COMPLETION: Create the descending index on price
Use db.books.createIndex() with the variable priceIndex to create a descending index on the price field.
MongoDB
Hint
Call db.books.createIndex() with priceIndex as the argument.
Practice
(1/5)
1. What does the index direction 1 mean in MongoDB indexes?
easy
A. Descending order
B. No order
C. Ascending order
D. Random order
Solution
Step 1: Understand index direction values
In MongoDB, index direction 1 means ascending order, and -1 means descending order.
Step 2: Match the value to the meaning
Since the question asks about 1, it corresponds to ascending order.
Final Answer:
Ascending order -> Option C
Quick Check:
Index direction 1 = Ascending order [OK]
Hint: 1 means ascending, -1 means descending [OK]
Common Mistakes:
Confusing 1 with descending order
Thinking 1 means no order
Mixing up ascending and descending
2. Which of the following is the correct syntax to create a descending index on the field age in MongoDB?
easy
A. db.collection.createIndex({ age: 1 })
B. db.collection.createIndex({ age: -1 })
C. db.collection.createIndex({ age: 'desc' })
D. db.collection.createIndex({ age: 'descending' })
Solution
Step 1: Recall MongoDB index syntax for direction
MongoDB uses 1 for ascending and -1 for descending in index creation.
Step 2: Identify the correct descending syntax
The correct way to create a descending index on age is { age: -1 }.
Final Answer:
db.collection.createIndex({ age: -1 }) -> Option B
Quick Check:
Descending index uses -1 [OK]
Hint: Use -1 for descending index direction [OK]
Common Mistakes:
Using string values like 'desc' instead of -1
Using 1 which means ascending
Incorrect object syntax
3. Given the index { name: 1, score: -1 }, which query will use this index efficiently?
medium
A. db.collection.find().sort({ name: 1, score: -1 })
B. db.collection.find().sort({ name: -1, score: 1 })
C. db.collection.find().sort({ score: -1, name: 1 })
D. db.collection.find().sort({ name: 1 })
Solution
Step 1: Understand compound index direction
The index sorts name ascending and score descending.
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 A
Quick Check:
Sort matches index directions exactly [OK]
Hint: Sort order must match index directions exactly [OK]
Common Mistakes:
Reversing sort directions
Changing field order in sort
Using partial sort that doesn't match index
4. You created an index with { date: 1, price: -1 } but your query sorts by { price: -1, date: 1 }. What is the problem?
medium
A. The index fields order does not match the sort order
B. The index direction values are invalid
C. MongoDB does not support mixed ascending and descending indexes
D. The query syntax is incorrect
Solution
Step 1: Check index field order
The index is created with fields date then price.
Step 2: Compare query sort order
The query sorts by price first, then date, which does not match the index field order.
Final Answer:
The index fields order does not match the sort order -> Option A
Quick Check:
Field order in sort must match index order [OK]
Hint: Index and sort field order must match exactly [OK]
Common Mistakes:
Assuming direction values are invalid
Thinking MongoDB disallows mixed directions
Blaming query syntax instead of order
5. You want to optimize queries that sort by category ascending and then by rating descending. Which index should you create?
hard
A. db.collection.createIndex({ rating: -1, category: 1 })
B. db.collection.createIndex({ category: -1, rating: 1 })
C. db.collection.createIndex({ rating: 1, category: -1 })
D. db.collection.createIndex({ category: 1, rating: -1 })
Solution
Step 1: Identify query sort order
The queries sort by category ascending and rating descending.
Step 2: Create matching compound index
The index must have category with direction 1 and rating with direction -1 in that order.
Final Answer:
db.collection.createIndex({ category: 1, rating: -1 }) -> Option D
Quick Check:
Index matches query sort directions and order [OK]
Hint: Match index fields and directions to query sort exactly [OK]
Common Mistakes:
Reversing field order in index
Swapping ascending and descending directions
Creating index on wrong fields
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.