0
0
MongoDBquery~20 mins

Compound index and field order in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Compound Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of field order in compound index on query performance

Consider a MongoDB collection with a compound index on { age: 1, score: -1 }. Which query will efficiently use this index?

MongoDB
db.collection.find({ age: 25, score: { $gte: 80 } })
AThe query uses the index efficiently because it matches the prefix fields in order.
BThe query does not use the index because the order of fields in the query is reversed.
CThe query uses the index only for the <code>score</code> field, ignoring <code>age</code>.
DThe query cannot use the index because it uses a range query on <code>score</code>.
Attempts:
2 left
💡 Hint

Think about how MongoDB uses the prefix of the compound index fields in order.

🧠 Conceptual
intermediate
1:30remaining
Understanding field order in compound indexes

Why does the order of fields in a MongoDB compound index matter?

ABecause MongoDB ignores the order and uses all fields equally in any query.
BBecause MongoDB can only use the index if queries filter on the first field(s) in the order defined.
CBecause the order determines the storage size of the index but not query usage.
DBecause the order affects only the write speed, not the read speed.
Attempts:
2 left
💡 Hint

Think about how indexes are structured and how queries match them.

📝 Syntax
advanced
2:00remaining
Correct syntax for creating a compound index with field order

Which of the following commands correctly creates a compound index on lastName ascending and age descending in MongoDB?

Adb.users.createIndex({ lastName: 1, age: -1 })
Bdb.users.createIndex(["lastName", 1], ["age", -1])
Cdb.users.createIndex({ age: -1, lastName: 1 })
Ddb.users.createIndex({ lastName: "asc", age: "desc" })
Attempts:
2 left
💡 Hint

Remember the syntax for createIndex uses a single object with field names and directions.

optimization
advanced
2:30remaining
Optimizing queries with compound index field order

You have a compound index on { city: 1, status: 1 }. Which query will NOT use this index efficiently?

Options:
A) db.collection.find({ city: "NY", status: "active" })
B) db.collection.find({ status: "active" })
C) db.collection.find({ city: "NY" })
D) db.collection.find({ city: "NY", status: { $in: ["active", "pending"] } })
AQuery filters on both city and status, uses index efficiently.
BQuery filters only on city, uses index efficiently.
CQuery filters only on status, cannot use index efficiently.
DQuery filters on city and status with $in, uses index efficiently.
Attempts:
2 left
💡 Hint

Recall that the index can only be used efficiently if the query filters on the first field in the index.

🔧 Debug
expert
3:00remaining
Diagnosing why a compound index is not used

A MongoDB collection has a compound index on { category: 1, price: 1 }. A query db.products.find({ price: { $lt: 100 } }) is slow and does not use the index. Why?

ABecause range queries on <code>price</code> prevent index usage.
BBecause the query uses a comparison operator which MongoDB does not support in indexes.
CBecause the index fields are in ascending order and the query needs descending order.
DBecause the query does not filter on the first field <code>category</code>, so the index is not used.
Attempts:
2 left
💡 Hint

Think about how MongoDB uses compound indexes and the importance of filtering on the first field.