Discover how a simple index can make your database searches feel instant!
Why Covered queries with indexes in MongoDB? - Purpose & Use Cases
Imagine you have a huge phone book and you want to find all people named "John" who live in "New York". Without any guide, you have to flip through every page, checking each entry one by one.
This manual search is slow and tiring. You might miss some entries or make mistakes. It wastes a lot of time, especially when the phone book is very thick.
Covered queries with indexes act like a special shortcut. They let you find exactly what you want by looking only at the index, without opening the full phone book. This makes searching super fast and efficient.
db.contacts.find({name: 'John', city: 'New York'})// Create an index that covers the query fields
db.contacts.createIndex({name: 1, city: 1, phone: 1})
db.contacts.find({name: 'John', city: 'New York'}, {phone: 1, _id: 0})It enables lightning-fast searches by using only the index, skipping the full data lookup.
Online stores use covered queries to quickly show you products matching your filters without scanning every product detail.
Manual searches scan all data and are slow.
Covered queries use indexes that include all needed fields.
This makes queries faster and reduces server work.
Practice
Solution
Step 1: Understand what a covered query means
A covered query uses an index that contains all the fields needed for the query, so MongoDB does not need to fetch the full document from the collection.Step 2: Identify the correct description
A query that uses an index to return all requested fields without reading the full document correctly describes a covered query as one that returns all requested fields from the index alone, improving speed and resource use.Final Answer:
A query that uses an index to return all requested fields without reading the full document -> Option DQuick Check:
Covered query = uses index only [OK]
- Thinking covered queries scan full documents
- Confusing covered queries with index-only updates
- Assuming covered queries only return _id field
status and returning status and score fields?Solution
Step 1: Identify fields needed for covered query
The query filters onstatusand returnsstatusandscore. The index must include all these fields.Step 2: Choose index covering all fields
{ status: 1, score: 1 } includes bothstatusandscore, so it can cover the query without fetching documents.Final Answer:
{ status: 1, score: 1 } -> Option AQuick Check:
Index includes filter and return fields [OK]
- Using index with only filter or only return fields
- Assuming _id is always included in covered queries
- Ignoring order of fields in index for coverage
{ category: 1, price: 1 } on a collection, what will be the output of this query?db.products.find({ category: 'books' }, { category: 1, price: 1, _id: 0 })Solution
Step 1: Analyze the index and query fields
The index coverscategoryandprice. The query filters oncategoryand returnscategoryandpriceonly, excluding_id.Step 2: Determine if query is covered
Since all requested fields are in the index, MongoDB can return results from the index without fetching full documents, making it a covered query.Final Answer:
Documents with only category and price fields, fetched from index (covered query) -> Option CQuick Check:
Index covers filter and projection fields [OK]
- Assuming _id exclusion causes error
- Thinking full documents are always returned
- Ignoring index coverage for projection
{ user: 1 } but your query is:db.users.find({ user: 'alice' }, { age: 1, _id: 0 })Why is this query NOT a covered query?
Solution
Step 1: Check index fields and query projection
The index has onlyuser. The query filters onuserbut only returnsage, excluding_id.Step 2: Understand covered query requirements
For a covered query, the index must include all returned fields in the projection. Sinceageis not in the index, MongoDB must fetch the full document.Final Answer:
Because the index does not include theagefield in the index keys -> Option AQuick Check:
Index must include all projection fields [OK]
- Thinking _id exclusion breaks coverage
- Assuming index on filter field covers all projections
- Thinking filter fields must appear in projection
db.orders.find({ status: 'shipped', total: { $gt: 100 } }, { status: 1, total: 1, customer: 1, _id: 0 })Which index will allow this query to be covered?
Solution
Step 1: Identify filter and projection fields
The query filters onstatusandtotal, and returnsstatus,total, andcustomer.Step 2: Choose index covering all fields
To cover the query, the index must include all filter and returned fields:status,total, andcustomer. { status: 1, total: 1, customer: 1 } includes all three.Final Answer:
{ status: 1, total: 1, customer: 1 } -> Option BQuick Check:
Index covers filter + projection fields [OK]
- Leaving out returned fields from index
- Assuming filter fields alone suffice
- Ignoring order of fields in index
Ready to prove it?
Pass the test and this pattern moves unknown
