Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand index direction values

    In MongoDB, index direction 1 means ascending order, and -1 means descending order.
  2. Step 2: Match the value to the meaning

    Since the question asks about 1, it corresponds to ascending order.
  3. Final Answer:

    Ascending order -> Option C
  4. 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

  1. Step 1: Recall MongoDB index syntax for direction

    MongoDB uses 1 for ascending and -1 for descending in index creation.
  2. Step 2: Identify the correct descending syntax

    The correct way to create a descending index on age is { age: -1 }.
  3. Final Answer:

    db.collection.createIndex({ age: -1 }) -> Option B
  4. 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

  1. Step 1: Understand compound index direction

    The index sorts name ascending and score descending.
  2. 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.
  3. Final Answer:

    db.collection.find().sort({ name: 1, score: -1 }) -> Option A
  4. 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

  1. Step 1: Check index field order

    The index is created with fields date then price.
  2. Step 2: Compare query sort order

    The query sorts by price first, then date, which does not match the index field order.
  3. Final Answer:

    The index fields order does not match the sort order -> Option A
  4. 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

  1. Step 1: Identify query sort order

    The queries sort by category ascending and rating descending.
  2. Step 2: Create matching compound index

    The index must have category with direction 1 and rating with direction -1 in that order.
  3. Final Answer:

    db.collection.createIndex({ category: 1, rating: -1 }) -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard