📖 Scenario: You are managing a small online bookstore database. You want to speed up queries that search for books by author and title without fetching the entire document from the database.
🎯 Goal: Create a MongoDB collection called books with sample data. Then create a covered query by building an index on author and title fields. Finally, write a query that uses this index to return only the author and title fields, ensuring the query is covered by the index.
📋 What You'll Learn
Create a books collection with 3 documents containing author, title, and year fields
Create a compound index on author and title
Write a query that finds books by a specific author and returns only author and title fields
Ensure the query is covered by the index (no document fetch needed)
💡 Why This Matters
🌍 Real World
Covered queries help speed up database reads by using indexes alone, which is important for fast search features in apps like online bookstores.
💼 Career
Understanding covered queries and indexes is essential for database administrators and backend developers to optimize query performance and reduce server load.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a MongoDB collection called books and insert these exact documents: { author: "George Orwell", title: "1984", year: 1949 }, { author: "Harper Lee", title: "To Kill a Mockingbird", year: 1960 }, and { author: "George Orwell", title: "Animal Farm", year: 1945 }.
MongoDB
Hint
Use db.books.insertMany([...]) with the exact documents inside the array.
2
Create a compound index on author and title
Create a compound index on the books collection for the fields author and title in ascending order.
MongoDB
Hint
Use db.books.createIndex({ author: 1, title: 1 }) to create the index.
3
Write a query to find books by George Orwell returning only author and title
Write a MongoDB query on the books collection to find documents where author is "George Orwell". Return only the author and title fields (exclude _id).
MongoDB
Hint
Use db.books.find({ author: "George Orwell" }, { author: 1, title: 1, _id: 0 }) to return only the needed fields.
4
Ensure the query is covered by the index
Add the .explain("executionStats") method to the query to check that the query is covered by the index (no document fetch).
MongoDB
Hint
Add .explain("executionStats") after the find() query to check the query plan.
Practice
(1/5)
1. What is a covered query in MongoDB?
easy
A. A query that only returns the document _id field
B. A query that scans all documents in a collection
C. A query that updates documents without using indexes
D. A query that uses an index to return all requested fields without reading the full document
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 D
Quick Check:
Covered query = uses index only [OK]
Hint: Covered queries return data only from indexes, no document fetch [OK]
Common Mistakes:
Thinking covered queries scan full documents
Confusing covered queries with index-only updates
Assuming covered queries only return _id field
2. Which MongoDB index definition supports a covered query for filtering on status and returning status and score fields?
easy
A. { status: 1, score: 1 }
B. { score: 1 }
C. { status: 1 }
D. { _id: 1, status: 1 }
Solution
Step 1: Identify fields needed for covered query
The query filters on status and returns status and score. The index must include all these fields.
Step 2: Choose index covering all fields
{ status: 1, score: 1 } includes both status and score, so it can cover the query without fetching documents.
Final Answer:
{ status: 1, score: 1 } -> Option A
Quick Check:
Index includes filter and return fields [OK]
Hint: Index must include all filter and return fields for coverage [OK]
Common Mistakes:
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
3. Given the index { category: 1, price: 1 } on a collection, what will be the output of this query?
A. Because the index does not include the age field in the index keys
B. Because the query excludes _id which is always required
C. Because the index does not include the age field in the projection
D. Because the query filters on user but projection excludes it
Solution
Step 1: Check index fields and query projection
The index has only user. The query filters on user but only returns age, excluding _id.
Step 2: Understand covered query requirements
For a covered query, the index must include all returned fields in the projection. Since age is not in the index, MongoDB must fetch the full document.
Final Answer:
Because the index does not include the age field in the index keys -> Option A
Quick Check:
Index must include all projection fields [OK]
Hint: All projected fields must be in the index for coverage [OK]
Common Mistakes:
Thinking _id exclusion breaks coverage
Assuming index on filter field covers all projections
The query filters on status and total, and returns status, total, and customer.
Step 2: Choose index covering all fields
To cover the query, the index must include all filter and returned fields: status, total, and customer. { status: 1, total: 1, customer: 1 } includes all three.
Final Answer:
{ status: 1, total: 1, customer: 1 } -> Option B
Quick Check:
Index covers filter + projection fields [OK]
Hint: Index must include all filter and returned fields for coverage [OK]
Common Mistakes:
Leaving out returned fields from index
Assuming filter fields alone suffice
Ignoring order of fields in index
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.