Record slow operations and query profiles in MongoDB

Configure MongoDB slow-operation logging and database profiling, then inspect system.profile without confusing it with the diagnostic log.

Best practices
Illustration of monitoring and troubleshooting application behavior

MongoDB's diagnostic log and database profiler are related but separate. The diagnostic log can contain slow operations even when profiling is disabled. The profiler writes selected operations to the database's system.profile collection on mongod.

Configure slow-operation profiling

For a self-managed mongod, an example configuration is:

systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true

operationProfiling:
  mode: slowOp
  slowOpThresholdMs: 100

Restart the relevant process to apply file configuration changes. Ensure its account can write the log path. Hosted services may expose different controls.

slowOp profiles selected slow operations; all profiles all operations and can be expensive. Setting profiling to all does not, by itself, turn the diagnostic log into a complete query history. Profiling is unavailable on mongos.

Change profiling at runtime

In mongosh, select the database explicitly:

use mydb
db.getProfilingStatus()
db.setProfilingLevel(1, { slowms: 100 })

// Use only for a bounded investigation.
// db.setProfilingLevel(2)

// Disable profiling when finished, or restore the previous level.
// db.setProfilingLevel(0)

The profiling level is database-specific, while the slow-operation threshold and sampling settings affect the process more broadly. Inspect existing filters and sampling before interpreting missing records: a profiling filter changes how operations are selected.

Threshold semantics are version-sensitive. Current MongoDB documentation describes slow-operation selection using working time (workingMillis), rather than treating every kind of waiting as query execution. Use the reference for the deployed version when comparing logged duration with client latency.

Inspect the records

// Most recent profiled operations
db.system.profile.find().sort({ ts: -1 }).limit(5).pretty()

// Longest recorded operations in the retained profile window
db.system.profile.find().sort({ millis: -1 }).limit(10)

// Counts by operation type
db.system.profile.aggregate([
  { $group: { _id: "$op", count: { $sum: 1 } } }
])

The diagnostic log may contain Slow query records. Do not expect an exact one-to-one match with system.profile: thresholds, sampling, filters, and profiling level determine what each destination contains.

For multiple instances, collect the diagnostic files with DataKit and retain instance identifiers. Verify one controlled slow operation's timestamp, duration, and query fields before comparing aggregated results. Query documents may contain sensitive values.

Common questions

How much does full profiling cost? Each profiled operation adds work and a profile record. Measure it under your workload and keep level 2 to a short investigation.

What threshold should I choose? Start from your latency objective. A 100 ms threshold may suit some interactive workloads, while analytical operations can legitimately take longer. Adjust based on the resulting volume and usefulness.

Does system.profile grow forever? No. It is a capped collection, normally created with a 1 MB default size, so older records are overwritten. Plan collection and retention before relying on it for historical analysis.

See MongoDB profiling controls for scope and selection rules.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting