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 }).

CategoryMonitoring & Diagnostics
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.runCommand({ profile: <level>, slowms: <ms> })
db.setProfilingLevel(1, { slowms: 200 })
db.getProfilingStatus()

⚙️ Parameters

profileLevel: 0 off, 1 slow ops only, 2 everything
slowmsSlow-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

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 }).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏