0
0
MongoDBquery~20 mins

Index direction (ascending vs descending) in MongoDB - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Direction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of ascending index on query sort

Given a MongoDB collection products with an ascending index on price, what will be the order of price values returned by this query?

db.products.find().sort({price: 1})
ADocuments sorted by price from lowest to highest
BDocuments sorted by price from highest to lowest
CDocuments returned in insertion order
DDocuments sorted randomly
Attempts:
2 left
💡 Hint

Ascending index means sorting from smallest to largest.

query_result
intermediate
2:00remaining
Effect of descending index on query sort

Given a MongoDB collection orders with a descending index on orderDate, what will be the order of orderDate values returned by this query?

db.orders.find().sort({orderDate: -1})
ADocuments sorted by orderDate ascending
BDocuments sorted by orderDate from oldest to newest
CDocuments returned in random order
DDocuments sorted by orderDate from newest to oldest
Attempts:
2 left
💡 Hint

Descending index means sorting from largest to smallest.

🧠 Conceptual
advanced
2:30remaining
Index direction impact on compound index queries

Consider a compound index on {category: 1, price: -1} in a MongoDB collection. Which query will benefit most from this index?

Adb.items.find().sort({category: -1, price: 1})
Bdb.items.find({category: 'books'}).sort({category: 1, price: -1})
Cdb.items.find({price: {$gt: 10}}).sort({price: -1})
Ddb.items.find({category: 'books'}).sort({price: 1})
Attempts:
2 left
💡 Hint

The query must match the index fields and directions to use the index efficiently.

📝 Syntax
advanced
1:30remaining
Correct syntax for creating descending index

Which of the following commands correctly creates a descending index on the score field in MongoDB?

Adb.scores.createIndex({score: 0})
Bdb.scores.createIndex({score: 'desc'})
Cdb.scores.createIndex({score: -1})
Ddb.scores.createIndex({score: 'descending'})
Attempts:
2 left
💡 Hint

Index direction is specified by 1 for ascending or -1 for descending.

optimization
expert
3:00remaining
Choosing index direction for frequent queries

You have a MongoDB collection events with frequent queries sorting by timestamp ascending and some queries sorting by timestamp descending. Which index direction should you choose to optimize overall performance?

ACreate an ascending index on timestamp to optimize ascending sorts; descending sorts will scan backward
BCreate a descending index on timestamp to optimize descending sorts; ascending sorts will scan backward
CCreate two separate indexes: one ascending and one descending on timestamp
DCreate a hashed index on timestamp for both ascending and descending sorts
Attempts:
2 left
💡 Hint

MongoDB can scan indexes backward to support opposite sort direction.