📖 Scenario: You are a database administrator for a small online store. You want to find out which queries are running slowly in your MongoDB database so you can improve performance.
🎯 Goal: Build a simple MongoDB profiler setup that records slow queries and then query the profiler data to find those slow queries.
📋 What You'll Learn
Enable the MongoDB profiler to log slow queries
Set a threshold for what counts as a slow query
Query the profiler collection to find slow queries
Limit the output to the slowest queries
💡 Why This Matters
🌍 Real World
Database administrators use profiling to identify slow queries that affect application performance and user experience.
💼 Career
Knowing how to use the MongoDB profiler is important for roles like DBA, backend developer, and performance engineer.
Progress0 / 4 steps
1
Enable the MongoDB profiler
Enable the MongoDB profiler on the current database with level 1 to log slow queries.
MongoDB
Hint
Use db.setProfilingLevel(1) to enable profiling of slow queries.
2
Set the slow query threshold
Set the slow query threshold to 100 milliseconds using slowms option in the profiler configuration.
MongoDB
Hint
Use db.setProfilingLevel(1, { slowms: 100 }) to set the threshold.
3
Query the profiler for slow queries
Write a query on the system.profile collection to find all queries where millis is greater than 100.
MongoDB
Hint
Use db.system.profile.find({ millis: { $gt: 100 } }) to find slow queries.
4
Sort and limit the slow queries output
Modify the query to sort the slow queries by millis in descending order and limit the output to 5 documents.
MongoDB
Hint
Use .sort({ millis: -1 }) and .limit(5) on the query.
Practice
(1/5)
1. What is the main purpose of the MongoDB profiler when set to capture slow queries?
easy
A. To increase the speed of all queries by caching results
B. To record queries that take longer than a specified time to help optimize performance
C. To delete slow queries from the database
D. To automatically fix slow queries without user intervention
Solution
Step 1: Understand profiler's role
The profiler records queries that exceed a time threshold to help identify slow operations.
Step 2: Identify what profiler does not do
It does not fix or delete queries automatically; it only logs them for review.
Final Answer:
To record queries that take longer than a specified time to help optimize performance -> Option B
Quick Check:
Profiler logs slow queries = To record queries that take longer than a specified time to help optimize performance [OK]
Hint: Profiler logs slow queries for review, not automatic fixes [OK]
Common Mistakes:
Thinking profiler fixes queries automatically
Confusing profiler with query caching
Assuming profiler deletes slow queries
2. Which command correctly sets the MongoDB profiler to log all queries slower than 100 milliseconds?
easy
A. db.setProfilingLevel(2, { slowms: 100 })
B. db.setProfilingLevel(0, { slowms: 100 })
C. db.setProfilingLevel(1, { slowms: 100 })
D. db.setProfilingLevel(1, 100)
Solution
Step 1: Recall profiling levels
Level 1 logs slow queries exceeding the slowms threshold.
Step 2: Check correct syntax
The correct syntax uses level and options object with slowms key, so db.setProfilingLevel(1, { slowms: 100 }) is valid.
Final Answer:
db.setProfilingLevel(1, { slowms: 100 }) -> Option C