📖 Scenario: You are managing a MongoDB database for an online bookstore. The database has a collection called books that stores information about each book, including its title, author, and genre. To improve query performance, you want to identify which indexes are missing based on the queries run on the collection.
🎯 Goal: Build a MongoDB query to find missing indexes by analyzing the system.profile collection. You will create a query to find slow queries that could benefit from indexes, then configure a threshold for slow queries, and finally write a query to list those queries missing indexes.
📋 What You'll Learn
Create a query to find slow queries in the system.profile collection
Set a threshold variable for slow query duration in milliseconds
Write a query to find queries that took longer than the threshold and did not use an index
Add a final query to list the fields used in those slow queries to help identify missing indexes
💡 Why This Matters
🌍 Real World
Database administrators often need to find missing indexes to improve query speed and reduce server load.
💼 Career
Knowing how to analyze query performance and identify missing indexes is a key skill for database developers and administrators.
Progress0 / 4 steps
1
Create a query to find slow queries in system.profile
Write a MongoDB query called slowQueries that finds all documents in the system.profile collection where the millis field is greater than 100. Use the db.system.profile.find() method with a filter object.
MongoDB
Hint
Use db.system.profile.find() with a filter object that checks if millis is greater than 100.
2
Set a threshold variable for slow query duration
Create a variable called slowThreshold and set it to 100 to represent the slow query duration threshold in milliseconds.
MongoDB
Hint
Declare a constant variable named slowThreshold and assign it the value 100.
3
Find queries slower than threshold without index usage
Write a MongoDB query called missingIndexQueries that finds documents in system.profile where millis is greater than slowThreshold and planSummary equals "COLLSCAN" (collection scan, meaning no index used). Use db.system.profile.find() with a filter object using slowThreshold.
MongoDB
Hint
Use db.system.profile.find() with a filter that checks millis > slowThreshold and planSummary equal to "COLLSCAN".
4
List fields used in slow queries missing indexes
Create a MongoDB aggregation called fieldsUsed on missingIndexQueries that groups by the command.filter field to identify which fields are used in slow queries without indexes. Use aggregate() with $group stage grouping by "$command.filter" and count the occurrences with $sum.
MongoDB
Hint
Use db.system.profile.aggregate() with $match to filter and $group to count by "$command.filter".
Practice
(1/5)
1. What is the main reason to create indexes in MongoDB collections?
easy
A. To store more data in the database
B. To make queries run faster by helping MongoDB find data quickly
C. To backup the database automatically
D. To encrypt the data for security
Solution
Step 1: Understand the purpose of indexes
Indexes are special data structures that help MongoDB quickly locate data without scanning the entire collection.
Step 2: Compare options with index purpose
Only To make queries run faster by helping MongoDB find data quickly correctly states that indexes speed up queries by helping find data faster.
Final Answer:
To make queries run faster by helping MongoDB find data quickly -> Option B
Quick Check:
Indexes improve query speed = A [OK]
Hint: Indexes speed up data search, not storage or security [OK]
Common Mistakes:
Thinking indexes store more data
Confusing indexes with backups
Assuming indexes encrypt data
2. Which MongoDB command lists all indexes on a collection named users?
easy
A. db.users.showIndexes()
B. db.users.getIndex()
C. db.users.findIndexes()
D. db.users.listIndexes()
Solution
Step 1: Recall the correct method to list indexes
The correct MongoDB method to list indexes on a collection is listIndexes().
Step 2: Check each option's validity
Only db.users.listIndexes() is a valid MongoDB command; others are incorrect or do not exist.
Final Answer:
db.users.listIndexes() -> Option D
Quick Check:
List indexes command = listIndexes() [OK]
Hint: Use listIndexes() to see all indexes on a collection [OK]
Common Mistakes:
Using getIndex() which is not a MongoDB method
Trying findIndexes() which does not exist
Confusing showIndexes() with listIndexes()
3. Given the query db.orders.find({ customerId: 123 }) and the collection has no index on customerId, what will explain('executionStats') likely show?
medium
A. A COLLSCAN (collection scan) with high number of documents examined
B. An IXSCAN (index scan) using the customerId index
C. A syntax error because explain cannot be used here
D. No documents returned because no index exists
Solution
Step 1: Understand query behavior without index
Without an index on customerId, MongoDB must scan the entire collection (COLLSCAN) to find matching documents.
Step 2: Interpret explain output
The explain('executionStats') will show a COLLSCAN and a high number of documents examined, indicating no index was used.
Final Answer:
A COLLSCAN (collection scan) with high number of documents examined -> Option A
Quick Check:
No index means COLLSCAN = B [OK]
Hint: No index means full collection scan in explain output [OK]
Common Mistakes:
Expecting an index scan without an index
Thinking explain causes syntax error
Assuming no documents returned without index
4. You notice a slow query on db.products.find({ category: 'books', price: { $lt: 20 } }). The collection has an index only on category. What is the best fix?
medium
A. Rewrite the query to remove the price filter
B. Remove the existing index on category
C. Add a compound index on { category: 1, price: 1 }
D. Add an index only on price
Solution
Step 1: Analyze existing index and query filters
The query filters on both category and price, but only category is indexed, so the price filter is not optimized.
Step 2: Choose the best index to optimize query
A compound index on { category: 1, price: 1 } allows MongoDB to efficiently filter by both fields, speeding up the query.
Final Answer:
Add a compound index on { category: 1, price: 1 } -> Option C
Quick Check:
Compound index matches query fields = D [OK]
Hint: Use compound indexes for queries filtering multiple fields [OK]
Common Mistakes:
Removing useful indexes
Adding index on only one field when query filters multiple
Changing query to remove filters instead of indexing
5. A MongoDB collection logs has slow queries filtering by userId and sorting by timestamp. Which index best improves performance?
hard
A. A compound index on { userId: 1, timestamp: -1 }
B. An index on { timestamp: 1 }
C. An index on { userId: 1 }
D. No index needed; increase RAM instead
Solution
Step 1: Understand query pattern with filter and sort
The query filters by userId and sorts by timestamp, so both fields affect performance.
Step 2: Select index that supports filter and sort
A compound index on { userId: 1, timestamp: -1 } supports efficient filtering by userId and sorting by timestamp in descending order.
Final Answer:
A compound index on { userId: 1, timestamp: -1 } -> Option A
Quick Check:
Compound index matches filter and sort = C [OK]
Hint: Compound index with sort field improves filter + sort queries [OK]
Common Mistakes:
Using single-field index ignoring sort
Choosing wrong sort order in index
Ignoring indexing and blaming hardware
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.