📖 Scenario: You are managing a small online bookstore using MongoDB. As the number of books and customers grows, you notice that some searches and updates are getting slower. To keep the store running smoothly and customers happy, you need to understand why performance tuning is important.
🎯 Goal: Build a simple MongoDB collection with book data, add an index to improve search speed, and observe how performance tuning helps queries run faster.
📋 What You'll Learn
Create a MongoDB collection named books with sample book documents
Add an index on the title field to speed up searches
Write a query to find books by title using the index
Explain why adding the index improves performance
💡 Why This Matters
🌍 Real World
Performance tuning is essential in real-world databases to keep applications fast and responsive as data grows.
💼 Career
Database administrators and developers use indexing and tuning to optimize queries and improve user experience.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a MongoDB collection called books and insert these exact documents: { "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925 }, { "title": "1984", "author": "George Orwell", "year": 1949 }, and { "title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960 }.
MongoDB
Hint
Use db.books.insertMany() with an array of book objects.
2
Add an index on the title field
Create an index on the title field of the books collection using db.books.createIndex to help speed up searches by book title.
MongoDB
Hint
Use db.books.createIndex({ title: 1 }) to create an ascending index on the title field.
3
Query books by title using the index
Write a query using db.books.find to find the book with the exact title "1984".
MongoDB
Hint
Use db.books.find({ title: "1984" }) to search for the book by title.
4
Explain why adding the index improves performance
Add a comment explaining that creating an index on the title field helps MongoDB find books faster by avoiding scanning every document.
MongoDB
Hint
Write a comment starting with // explaining how the index speeds up searches.
Practice
(1/5)
1. Why is performance tuning important in MongoDB databases?
easy
A. It helps queries run faster and reduces resource use.
B. It increases the size of the database automatically.
C. It deletes old data to save space.
D. It changes the database schema without user input.
Solution
Step 1: Understand performance tuning purpose
Performance tuning aims to make database operations faster and more efficient.
Step 2: Identify the correct benefit in MongoDB context
Faster queries and less resource use directly improve application speed and cost.
Final Answer:
It helps queries run faster and reduces resource use. -> Option A
Quick Check:
Performance tuning = faster queries [OK]
Hint: Performance tuning improves speed and resource use [OK]
Common Mistakes:
Confusing tuning with data deletion
Thinking tuning changes schema automatically
Believing tuning increases database size
2. Which of the following is the correct way to create an index on the username field in MongoDB?
easy
A. db.collection.index({username: 'asc'})
B. db.collection.create_index('username')
C. db.collection.createIndex({username: 1})
D. db.collection.addIndex(username)
Solution
Step 1: Recall MongoDB index creation syntax
MongoDB uses createIndex with a document specifying fields and sort order (1 for ascending).
Step 2: Match syntax with options
db.collection.createIndex({username: 1}) matches the correct syntax: db.collection.createIndex({username: 1}).
Final Answer:
db.collection.createIndex({username: 1}) -> Option C
Quick Check:
Correct index syntax = db.collection.createIndex({username: 1}) [OK]
Hint: Use createIndex with field and 1 or -1 [OK]
Common Mistakes:
Using wrong method names like create_index
Passing field name as string without object
Using addIndex which does not exist
3. Given the following MongoDB query, what is the expected result?
Hint: Explain shows query stats, not data changes [OK]
Common Mistakes:
Thinking explain modifies data
Assuming explain creates indexes
Confusing query filter with data update
4. You wrote this MongoDB query to find users older than 25:
db.users.find({age: {$gt: 25}})
But it runs very slowly. What is the most likely reason?
medium
A. The age field is not indexed.
B. The query syntax is incorrect.
C. MongoDB does not support the $gt operator.
D. The collection name is wrong.
Solution
Step 1: Check query syntax correctness
The syntax is correct; $gt is a valid operator and collection name seems fine.
Step 2: Identify performance cause
Without an index on age, MongoDB must scan all documents, causing slow queries.
Final Answer:
The age field is not indexed. -> Option A
Quick Check:
Missing index = slow query [OK]
Hint: Slow queries often mean missing indexes [OK]
Common Mistakes:
Blaming syntax when it's correct
Thinking $gt is unsupported
Assuming wrong collection name without checking
5. You have a large MongoDB collection with millions of documents. You want to improve query speed for searches on email and status. Which approach best improves performance?
hard
A. Remove all indexes to speed up writes.
B. Create separate indexes on email and status fields.
C. Store email and status in a single string field.
D. Create a compound index on {email: 1, status: 1}.
Solution
Step 1: Understand compound vs separate indexes
A compound index on both fields allows efficient queries filtering on both email and status together.
Step 2: Evaluate options for best performance
Separate indexes help single-field queries but compound index is better for combined filters. Storing combined fields as string or removing indexes harms performance.
Final Answer:
Create a compound index on {email: 1, status: 1}. -> Option D
Quick Check:
Compound index = best multi-field query speed [OK]
Hint: Use compound indexes for multi-field queries [OK]
Common Mistakes:
Using separate indexes for combined queries
Combining fields into one string reduces query flexibility
Removing indexes slows queries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.