0
0
MongoDBquery~30 mins

Index usage verification in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Index Usage Verification in MongoDB
📖 Scenario: You are managing a MongoDB database for a small online bookstore. To make searching for books faster, you want to use indexes on certain fields.
🎯 Goal: Learn how to create an index on a MongoDB collection and verify that the index is being used by queries.
📋 What You'll Learn
Create a MongoDB collection called books with sample book documents
Create an index on the author field
Run a query filtering by author and verify index usage with explain()
Check the query plan to confirm the index is used
💡 Why This Matters
🌍 Real World
Indexes in databases help speed up searches, just like an index in a book helps you find pages quickly.
💼 Career
Database administrators and backend developers use index creation and verification to optimize application performance.
Progress0 / 4 steps
1
Create the books collection with sample data
Insert three documents into the books collection with these exact fields and values: { title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937 }, { title: "1984", author: "George Orwell", year: 1949 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960 }.
MongoDB
Need a hint?

Use db.books.insertMany() with an array of objects for the books.

2
Create an index on the author field
Create an ascending index on the author field in the books collection using db.books.createIndex().
MongoDB
Need a hint?

Use db.books.createIndex({ author: 1 }) to create an ascending index on the author field.

3
Run a query filtering by author and use explain()
Write a query to find all books where author is exactly "George Orwell" using db.books.find(). Chain .explain() to this query to get the query plan.
MongoDB
Need a hint?

Use db.books.find({ author: "George Orwell" }).explain() to see the query plan.

4
Verify the index is used in the query plan
Check the query plan output from explain() and add a comment line that includes the phrase "IXSCAN" to indicate the index scan is used.
MongoDB
Need a hint?

Look for IXSCAN in the explain output and add a comment like // The query plan shows an IXSCAN.