The Database Profiler and Slow Query Log
Learners will enable the profiler, set a slowms threshold, and query system.profile to find the most expensive operations.
Why Profiling Matters
MongoDB can run thousands of queries per second, but a handful of slow queries can drag down an entire application. The database profiler and the slow query log are your primary tools for finding these expensive operations. They record query execution details — duration, documents examined, index usage — so you can identify and fix bottlenecks before users feel them.
Profiler Levels: 0, 1, and 2
The profiler has three levels: Level 0 — off, nothing is recorded. Level 1 — records operations that take longer than the slowms threshold (default 100 ms). This is the recommended production setting. Level 2 — records every operation regardless of duration. Level 2 is useful during debugging but creates too much write overhead for sustained production use.
// Enable level 1 profiling with 50ms threshold
db.setProfilingLevel(1, { slowms: 50 })
// Enable level 2 (capture everything)
db.setProfilingLevel(2)
// Turn profiling off
db.setProfilingLevel(0)All lessons in this course
- The Database Profiler and Slow Query Log
- Compound Index Prefix Rule and ESR Principle
- Index Intersection vs Compound Indexes
- Aggregation Pipeline Optimization Tips