Covered Queries
Covered Queries: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 48 of 100
Covered Queries
Foundations & CRUD ✓ → Queries & Schema → Aggregation & Scale → Atlas & Projects
Queries & Schema · 2 — Design · ~6 min · MongoDB — Indexing & Performance
What is this?
A covered query is satisfied entirely from an index — MongoDB never fetches the full documents. Projection must include only indexed fields (and usually exclude _id or index it too).
Why should you care?
Ultra-hot lookups (session token → userId) become much faster and cheaper when covered.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.users.createIndex({ email: 1, username: 1 })
db.users.insertOne({ email: "c@nosqlverse.dev", username: "cj", city: "Kochi" })
db.users.find(
{ email: "c@nosqlverse.dev" },
{ email: 1, username: 1, _id: 0 }
).explain("executionStats")
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- The compound index holds email and username.
- The query filters on email and returns only those fields with _id: 0, so totalDocsExamined can be 0 while totalKeysExamined > 0 — a covered plan.
Practice next
- Create the index and insert a user.
- Run the projected find with explain.
- Add city to the projection and see document fetch return.
- Cover a token → userId map collection.
- Compare latency with and without city in projection.
Remember
Covered queries read only from indexes. Projection must match indexed fields. Great for tiny hot lookups.
API key validation
Gateway checks apiKeys index for key hash → tenantId without loading the full key document.
Outcome: Auth overhead stays tiny at high QPS.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!