Navigation

db.setProfilingLevel()

On this page

Definition

db.setProfilingLevel(level, options)

Changed in version 4.0: The method can be run on mongos with profile level 0 to set the slowms and sampleRate for the diagnostic log; i.e. you cannot enable the profiler on a mongos.

For a mongod instance, the method configures the database profiler. If the profiler is disabled, the method sets the slowms and sampleRate for logging slow operations to the diagnostic log.

For mongos instance, the command sets the slowms and sampleRate for the diagnostic log.

This method provides a wrapper around the profile command.

Parameter Type Description
level integer

Configures the profiler level. The following profiler levels are available:

Level Description
0 The profiler is off and does not collect any data. This is the default profiler level.
1 The profiler collects data for operations that take longer than the value of slowms.
2 The profiler collects data for all operations.

Since profiling is not available on mongos, db.setProfilingLevel() cannot be used to set the profiling level to a value other than 0 on a mongos instance.

options document or integer

Optional. Accepts an integer or an options document. If an integer value is passed as the options argument instead of a document, the value is assigned to slowms. The following options are available:

slowms
Default: 100
Type: integer

The slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow.

When logLevel is set to 0, MongoDB records slow operations to the diagnostic log at a rate determined by slowOpSampleRate. At higher logLevel settings, all operations appear in the diagnostic log regardless of their latency.

For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.

For mongos instances, the setting affects the diagnostic log only and not the profiler since profiling is not available on mongos.

Note

This argument affects the same setting as the configuration file option slowOpThresholdMs.

sampleRate
Default: 1.0
Type: double

The fraction of slow operations that should be profiled or logged. sampleRate accepts values between 0 and 1, inclusive.

For mongod instances, the setting affects both the diagnostic log and, if enabled, the profiler.

For mongos instances, the setting affects the diagnostic log only and not the profiler since profiling is not available on mongos.

Note

This argument affects the same setting as the configuration option slowOpSampleRate.

Important

Profiling can impact performance and shares settings with the system log. Carefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.

See Profiler Overhead for more information on potential performance degradation.

To check the profiling level, see db.getProfilingStatus().