0
0
MongoDBquery~20 mins

Covered queries with indexes in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Covered Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a covered query using a compound index

Given a MongoDB collection products with documents containing category, price, and name fields, and a compound index on { category: 1, price: 1 }, what will be the output of the following query?

db.products.find({ category: 'books' }, { category: 1, price: 1, _id: 0 })

Assuming the collection has these documents:

[{ category: 'books', price: 10, name: 'Book A' }, { category: 'books', price: 15, name: 'Book B' }, { category: 'electronics', price: 100, name: 'Gadget' }]
MongoDB
db.products.find({ category: 'books' }, { category: 1, price: 1, _id: 0 })
A[{ category: 'books', price: 10 }, { category: 'books', price: 15 }]
B[{ category: 'books', price: 10, name: 'Book A' }, { category: 'books', price: 15, name: 'Book B' }]
C[{ category: 'books' }, { category: 'books' }]
D[{ price: 10 }, { price: 15 }]
Attempts:
2 left
💡 Hint

Remember that a covered query returns only the fields in the index and the projection.

🧠 Conceptual
intermediate
1:30remaining
Understanding covered queries and index fields

Which of the following statements about covered queries in MongoDB is TRUE?

AA covered query can return fields not included in the index if they are in the projection.
BCovered queries always require scanning the entire collection.
CA covered query requires all fields in the query filter and projection to be in the index.
DCovered queries do not improve query performance.
Attempts:
2 left
💡 Hint

Think about what fields the index must contain for MongoDB to avoid fetching documents.

📝 Syntax
advanced
2:00remaining
Identify the correct index definition for a covered query

You want to create an index on the users collection to cover this query:

db.users.find({ age: { $gte: 18 } }, { age: 1, name: 1, _id: 0 })

Which index definition will allow this query to be covered?

A{ age: 1, email: 1 }
B{ age: 1, name: 1 }
C{ name: 1 }
D{ age: 1 }
Attempts:
2 left
💡 Hint

The index must include all fields used in the filter and projection.

optimization
advanced
2:30remaining
Optimizing a query with covered indexes

You have a collection orders with fields customerId, status, and total. You run this query:

db.orders.find({ status: 'shipped' }, { customerId: 1, total: 1, _id: 0 })

Which index will best optimize this query as a covered query?

A{ status: 1 }
B{ total: 1, status: 1 }
C{ customerId: 1, total: 1 }
D{ status: 1, customerId: 1, total: 1 }
Attempts:
2 left
💡 Hint

The index must include all fields in the filter and projection, with the filter field first.

🔧 Debug
expert
3:00remaining
Why is this query not covered despite the index?

You created this index on employees collection:

{ department: 1, salary: 1 }

And run this query:

db.employees.find({ department: 'HR' }, { department: 1, salary: 1, name: 1, _id: 0 })

Why is this query NOT covered?

ABecause the projection includes <code>name</code> which is not in the index.
BBecause the filter on <code>department</code> is not supported by the index.
CBecause the index does not include the <code>_id</code> field.
DBecause the query does not specify <code>_id: 1</code> in the projection.
Attempts:
2 left
💡 Hint

Check if all projected fields are in the index.