0
0
MongoDBquery~20 mins

Index usage verification in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Check index usage with explain()
Given a MongoDB collection users with an index on age, what does the explain() output show when running db.users.find({age: 30}).explain()?
MongoDB
db.users.find({age: 30}).explain()
AThe output shows an error because <code>explain()</code> cannot be used with <code>find()</code>.
BThe output is empty because no documents match <code>age: 30</code>.
CThe output shows <code>stage: "COLLSCAN"</code> indicating a full collection scan was done.
DThe output shows <code>stage: "IXSCAN"</code> indicating the index on <code>age</code> was used.
Attempts:
2 left
💡 Hint
Look for the stage field in the winningPlan part of the explain output.
query_result
intermediate
2:00remaining
Effect of missing index on query plan
If a MongoDB collection orders has no index on customerId, what will db.orders.find({customerId: 123}).explain() show?
MongoDB
db.orders.find({customerId: 123}).explain()
AThe explain output shows <code>stage: "COLLSCAN"</code> indicating a full collection scan.
BThe explain output shows <code>stage: "IXSCAN"</code> even without an index.
CThe explain output shows <code>stage: "FETCH"</code> without scanning.
DThe query will fail with an error about missing index.
Attempts:
2 left
💡 Hint
Without an index, MongoDB must scan all documents to find matches.
📝 Syntax
advanced
2:00remaining
Identify correct syntax to create a compound index
Which option correctly creates a compound index on category ascending and price descending in MongoDB?
Adb.products.createIndex({category => 1, price => -1})
Bdb.products.createIndex([category: 1, price: -1])
Cdb.products.createIndex({category: 1, price: -1})
Ddb.products.createIndex({category: "asc", price: "desc"})
Attempts:
2 left
💡 Hint
Use an object with field names as keys and 1 or -1 for ascending or descending.
optimization
advanced
2:00remaining
Optimize query with index for range and equality
Given a collection sales with an index on {region: 1, date: 1}, which query will use the index efficiently?
Adb.sales.find({region: "West", date: {$gte: ISODate("2023-01-01")}})
Bdb.sales.find({date: {$gte: ISODate("2023-01-01")}, region: "West"})
Cdb.sales.find({region: {$gte: "West"}, date: ISODate("2023-01-01")})
Ddb.sales.find({region: "West"})
Attempts:
2 left
💡 Hint
The index fields order matters: equality on first field then range on second uses index best.
🧠 Conceptual
expert
3:00remaining
Understanding index intersection
If a MongoDB collection has separate indexes on status and priority, which query can benefit from index intersection?
Adb.tasks.find({status: "open"})
Bdb.tasks.find({status: "open", priority: "high"})
Cdb.tasks.find({priority: "high"})
Ddb.tasks.find({status: "open", priority: {$gt: 5}})
Attempts:
2 left
💡 Hint
Index intersection combines multiple single-field indexes to answer queries with multiple conditions.