Covered queries with indexes in MongoDB - Time & Space Complexity
When using covered queries with indexes, we want to know how fast MongoDB can find and return data without extra work.
We ask: How does the time to get results change as the data grows?
Analyze the time complexity of the following MongoDB query using a covered index.
db.users.createIndex({ email: 1, name: 1 })
const result = db.users.find(
{ email: "user@example.com" },
{ email: 1, name: 1, _id: 0 }
)
This query uses an index on email and name to return only those fields, so MongoDB does not need to look up the full documents.
Look for repeated steps that affect speed.
- Primary operation: Searching the index tree to find matching entries.
- How many times: Once per query, as the index is searched efficiently.
The time to find a matching entry grows slowly even if the data grows a lot.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3-4 steps to find the entry |
| 100 | About 5-6 steps |
| 1000 | About 7-8 steps |
Pattern observation: The steps grow slowly, not directly with the number of entries, because the index is like a tree that narrows down quickly.
Time Complexity: O(log n)
This means the time to find data grows slowly as the data grows, thanks to the index structure.
[X] Wrong: "Using an index means the query always takes the same time no matter how big the data is."
[OK] Correct: Even with indexes, the search takes more steps as data grows, but it grows slowly, not instantly.
Understanding how covered queries use indexes to speed up searches shows you know how databases handle data efficiently, a key skill for real projects.
What if the query requested fields not included in the index? How would the time complexity change?
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
