db.setProfilingLevel()
On this page
Definition
db.setProfilingLevel(level, options)
Changed in version 5.0.
For a
mongod
instance, the method enables, disables, or configures the Database Profiler. The profiler captures and records data on the performance of write operations, cursors, and database commands on a runningmongod
instance. If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.Note
Changes made to the profiling level with
db.setProfilingLevel()
do not persist. When the server restarts, it reverts to0
(the default), or the value set by either theoperationProfiling.mode
setting or the--profile
command-line option.If the database profiler level is
1
or2
(specifically, the database profiler is enabled), the slowms, sampleRate affect the behavior of both the profiler and thediagnostic log
.If the database profiler level is
0
(specifically, database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.With
mongos
instances, the method sets theslowms
,sampleRate
andfilter
configuration settings, which configure how operations get written to the diagnostic log. You cannot enable the Database Profiler on amongos
instance becausemongos
does not have any collections that the profiler can write to. Theprofile
level must be0
for amongos
instance.You can specify a filter on both
mongod
andmongos
instances to control which operations are logged by the profiler. When you specify afilter
for the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.db.setProfilingLevel()
provides a wrapper around theprofile
command.Starting in MongoDB 5.0, changes made to the database profiler
level
,slowms
,sampleRate
, orfilter
using theprofile
command ordb.setProfilingLevel()
wrapper method are recorded in thelog file
.
Compatibility
This method is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Important
This command is not supported in M0, M2, and M5 clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The db.setProfilingLevel()
method has the following form:
db.setProfilingLevel(<level>, <options>)
Parameters
Parameter | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
integer | Configures the database profiler level. The following profiler levels are available:
Because profiling is not available on | |||||||
document or integer | Optional. Accepts an integer or an options document. If an integer value is
passed as the
|
Returns
The method returns a document that contains the previous values of the settings.
{ "was" : 2, "slowms" : 100, "sampleRate" : 1, "filter" : { "$and" : [ { "op" : { "$eq" : "query" } }, { "millis" : { "$gt" : 20000 } } ] }, "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.", "ok" : 1 }
{ "was" : 0, "slowms" : 100, "sampleRate" : 1, "filter" : { "$and" : [ { "op" : { "$eq" : "query" } }, { "millis" : { "$gte" : 2000 } } ] }, "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.", "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, "filter" : { "$and" : [ { "op" : { "$eq" : "query" } }, { "millis" : { "$gte" : 2000 } } ] }, "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.", "ok" : 1, "operationTime" : Timestamp(1572991499, 2), "$clusterTime" : { "clusterTime" : Timestamp(1572991499, 2), "signature" : { "hash" : BinData(0,"nhCquIxUw7thlrBudXe3PnsnvP0="), "keyId" : NumberLong("6755946491040235540") } } }
Where:
was
is the previous level setting.slowms
is the previous slowms setting.sampleRate
is the previous sampleRate setting.filter
is the previous filter setting.note
is a string explaining the behavior offilter
. This field only appears in the output whenfilter
is also present.
Note
The filter
and note
fields only appear in the output if
they were present in the previous level setting.
To view the current profiling level, see db.getProfilingStatus()
.
Behavior
Warning
Profiling can degrade performance and expose unencrypted query data in 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.
Examples
Enable Profiler and Set Slow Operation Threshold and Sample Rate
The following example sets for a mongod
instance:
the profiling level to
1
,the slow operation threshold slowms to
20
milliseconds, andthe sampleRate to
0.42
.
db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
The operation returns a document with the previous values for the settings.
To view the current profiling level, see
db.getProfilingStatus()
.
Disable Profiler and Set Slow Operation Threshold and Sample Rate
The following example sets for a mongod
or
mongos
instance:
the profiling level to
0
,the slow operation threshold slowms to
20
milliseconds, andthe sampleRate to
0.42
.
db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
The operation returns a document with the previous values for the settings.
To view the current profiling level, see
db.getProfilingStatus()
.
Set a Filter to Determine Profiled Operations
The following example sets for a mongod
instance:
the profiling level to
1
,a filter of
{ op: "query", millis: { $gt: 2000 } }
, which causes the profiler to only recordquery
operations that took longer than 2 seconds.
db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )
The operation returns a document with the previous values for the settings.
To view the current profiling level, see
db.getProfilingStatus()
.
Unset a Filter
To clear a profile filter, run db.setProfilingLevel()
with the
filter: "unset"
option.
db.setProfilingLevel( 1, { filter: "unset" } )
The operation returns a document with the previous values for the settings.
To view the current profiling level, see
db.getProfilingStatus()
.