0
0
MongoDBquery~20 mins

Why advanced indexing matters in MongoDB - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Advanced Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents using a compound index
Given a MongoDB collection users with a compound index on { age: 1, city: 1 }, what is the output of this query?
db.users.find({ age: 30, city: "New York" }).explain("executionStats")

Which option correctly describes the query plan's index usage?
AThe query scans the entire collection because the index is not used for equality matches.
BThe query uses only the index on age and ignores the city field.
CThe query uses the compound index to efficiently find documents matching both age and city.
DThe query uses a text index instead of the compound index.
Attempts:
2 left
💡 Hint
Think about how compound indexes work with multiple fields in the query.
🧠 Conceptual
intermediate
1:30remaining
Why use multikey indexes?
In MongoDB, which situation requires a multikey index to improve query performance?
AWhen indexing a field with only numeric values.
BWhen indexing a single string field with unique values.
CWhen indexing a field that is always null.
DWhen indexing a field that contains an array of values.
Attempts:
2 left
💡 Hint
Think about how MongoDB indexes array fields.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax for creating a partial index
Which option shows the correct MongoDB command to create a partial index on the orders collection for documents where status is "pending"?
Adb.orders.createIndex({ orderDate: 1 }, { partialFilterExpression: { status: "pending" } })
Bdb.orders.createIndex({ orderDate: 1 }, { partialFilterExpression: { status: { $eq: "pending" } } })
Cdb.orders.createIndex({ orderDate: 1 }, { filter: { status: "pending" } })
Ddb.orders.createIndex({ orderDate: 1 }, { partialFilter: { status: "pending" } })
Attempts:
2 left
💡 Hint
Check the exact option name and syntax for partialFilterExpression.
optimization
advanced
2:00remaining
Choosing the best index for a frequent query
You have a collection products with fields category, price, and stock. The most frequent query is:
db.products.find({ category: "electronics", price: { $lt: 500 } })

Which index will optimize this query best?
AA compound index on { category: 1, price: 1 }
BA single field index on category only
CA single field index on price only
DA compound index on { price: 1, stock: 1 }
Attempts:
2 left
💡 Hint
Think about which fields appear in the query filter and their order in the index.
🔧 Debug
expert
2:30remaining
Why does this index not improve query performance?
A developer created this index:
db.sales.createIndex({ date: 1, total: 1 })

But the query
db.sales.find({ total: { $gt: 100 } })

still performs a collection scan. Why?
ABecause the query does not specify the first field of the compound index (date), the index cannot be used.
BBecause the index fields are in ascending order and the query needs descending order.
CBecause the query uses a range operator on total, which is the second field, so the index is ignored.
DBecause the index was created incorrectly and needs a unique constraint.
Attempts:
2 left
💡 Hint
Remember how MongoDB uses the prefix of compound indexes.