0
0
MongoDBquery~30 mins

Profiler for slow queries in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Profiler for Slow Queries in MongoDB
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use .sort({ millis: -1 }) and .limit(5) on the query.