db.setProfilingLevel()
Definition
db.
setProfilingLevel
(level, options)- The method configures database profiler level, the slowms, and the sampleRate.
If the database profiler level is1
or 2
(i.e. the database profiler is enabled), theslowms and thesampleRate affectthe behavior of both the profiler and the diagnostic log
.
If the database profiler level is0
(i.e. database profiler is disabled), theslowms and thesampleRate affectonly the diagnostic log.
Although profiling is unavailable on mongos
instance,starting in MongoDB 4.0, you can rundb.setProfilingLevel()
on mongos
to set theslowms andsampleRate for thediagnostic log. That is, for a mongos
instance, youmust specify 0
for level.
db.setProfilingLevel()
provides a wrapper around theprofile
command.
Syntax
The db.setProfilingLevel()
method has the following form:
- db.setProfilingLevel(<level>, <options>)
Parameters
Parameter | Type | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
level | integer | Configures the database profiler level. The followingprofiler levels are available:
Since profiling is not available on | ||||||||
options | document or integer | Optional. Accepts an integer or an options document. If an integer value ispassed as the options argument instead of a document, the value isassigned to slowms.The following options are available:
|
Returns
The method returns a document that contains the previous values ofthe settings.
- Standalone
- Replica Set Member
- mongos Instance
- { "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1 }
- {
- "was" : 0,
- "slowms" : 100,
- "sampleRate" : 1,
- "ok" : 1,
- "$clusterTime" : {
- "clusterTime" : Timestamp(1572991238, 1),
- "signature" : {
- "hash" : BinData(0,"hg6GnlrVhV9MAhwWdeHmHQ4T4qU="),
- "keyId" : NumberLong("6755945537557495811")
- }
- },
- "operationTime" : Timestamp(1572991238, 1)
- }
- {
- "was" : 0,
- "slowms" : 100,
- "sampleRate" : 1,
- "ok" : 1,
- "operationTime" : Timestamp(1572991499, 2),
- "$clusterTime" : {
- "clusterTime" : Timestamp(1572991499, 2),
- "signature" : {
- "hash" : BinData(0,"nhCquIxUw7thlrBudXe3PnsnvP0="),
- "keyId" : NumberLong("6755946491040235540")
- }
- }
- }
Where:
was
is the previouslevelsetting.slowms
is the previousslowms setting.sampleRate
is the previoussampleRate setting.
To view the current profiling level, see db.getProfilingStatus()
.
Behavior
Important
Profiling can impact performance and shares settings with the systemlog. Carefully consider any performance and security implicationsbefore configuring and enabling the profiler on a productiondeployment.
See Profiler Overhead for more information onpotential performance degradation.
Examples
Enable Profiler and Set Slow Operation Threshhold and Sample Rate
The following example sets for a mongod
instance:
- the profiling level to
1
, - the slow operation threshold slowms to
20
milliseconds, and - the sampleRate to
0.42
.
- db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
The method returns a document with the previous values for thesettings.
To view the current profiling level, see db.getProfilingStatus()
.
Disable Profiler and Set Slow Operation Threshhold and Sample Rate
The following example sets for a mongod
ormongos
instance:
- the profiling level to
0
, - the slow operation threshold slowms to
20
milliseconds, and - the sampleRate to
0.42
.
- db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
The method returns a document with the previous values for thesettings.
To view the current profiling level, see db.getProfilingStatus()
.