profile (Database Profiler)
Last updated: 2026-10-09
profile (Database Profiler)
profile toggles the Database Profiler: level 0 off, 1 records only slow operations (past slowms), 2 records everything. Logged operations land in system.profile, handy for locating bottlenecks. mongosh offers helpers like db.setProfilingLevel(1, { slowms: 200 }).
| Category | Monitoring & Diagnostics |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.runCommand({ profile: <level>, slowms: <ms> })
db.setProfilingLevel(1, { slowms: 200 })
db.getProfilingStatus()
⚙️ Parameters
| profile | Level: 0 off, 1 slow ops only, 2 everything |
|---|---|
| slowms | Slow-query threshold in ms, used at level 1 |
Returns:Returns the previous level (was), current slowms, and ok
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// profile: enable slow-op logging
use shop
db.runCommand({ profile: 1, slowms: 200 })
db.system.profile.find({ millis: { $gte: 200 } }).sort({ ts: -1 }).limit(5)
Output:
{ was: 0, slowms: 100, sampleRate: 1, ok: 1 }
// The query returns the latest 5 operations taking 200ms or more
📌 Notes
- Level 1 logs only operations past slowms; level 2 logs everything
- Profiler output lands in system.profile — clean it periodically
- In production prefer level 1 with a sane slowms to limit overhead
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between profile levels 1 and 2?
ALevel 1 logs only operations slower than slowms; level 2 logs everything.
QWhere does the profiler store its logs?
AIn the system.profile collection, queryable directly.
QHow do I turn the profiler off?
ARun db.setProfilingLevel(0) or db.runCommand({ profile: 0 }).