- Reference >
mongoshMethods >- Database Methods >
- db.setProfilingLevel()
db.setProfilingLevel()¶
On this page
Definition¶
-
db.setProfilingLevel(level, options)¶ Changed in version 5.0.
For a
mongodinstance, 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 runningmongodinstance. If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.If the database profiler level is
1or2(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.Starting in MongoDB 4.0, for a
mongosinstance, the method sets theslowms,sampleRateandfilterconfiguration settings, which configure how operations get written to the diagnostic log. You cannot enable the Database Profiler on amongosinstance becausemongosdoes not have any collections that the profiler can write to. Theprofilelevel must be0for amongosinstance.Starting in MongoDB 4.4.2, you can specify a filter on both
mongodandmongosinstances to control which operations are logged by the profiler. When you specify afilterfor the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.db.setProfilingLevel()provides a wrapper around theprofilecommand.Starting in MongoDB 5.0 (also available starting in 4.4.2, 4.2.12, and 4.0.22), changes made to the database profiler
level,slowms,sampleRate, orfilterusing theprofilecommand ordb.setProfilingLevel()wrapper method are recorded in thelog file.
Syntax¶
The db.setProfilingLevel() method has the following form:
Parameters¶
| Parameter | Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| level | integer | Configures the database profiler level. The following profiler levels are available:
Because profiling is not available on |
||||||||
| options | 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.
- Standalone
- Replica Set Member
- mongos Instance
Where:
wasis the previous level setting.slowmsis the previous slowms setting.sampleRateis the previous sampleRate setting.filteris the previous filter setting. (New in MongoDB 4.4.2)noteis a string explaining the behavior offilter. This field only appears in the output whenfilteris also present. (New in MongoDB 4.4.2)
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¶
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.
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
20milliseconds, and - the sampleRate to
0.42.
The method 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
20milliseconds, and - the sampleRate to
0.42.
The method 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¶
New in version 4.4.2.
The following example sets for a mongod instance:
- the profiling level to
2, - a filter of
{ op: "query", millis: { $gt: 2000 } }, which causes the profiler to only recordqueryoperations that took longer than 2 seconds.
The method returns a document with the previous values for the settings.
To view the current profiling level, see db.getProfilingStatus().