On this page本页内容
db.setProfilingLevel(level, options)¶Changed in version 4.4.2.
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 running mongod instance. If the profiler is disabled, the method configures how slow operations are logged to the diagnostic log.
If the database profiler level is 1 or 2 (i.e. the database profiler is enabled), the slowms, sampleRate affect the behavior of both the profiler and the diagnostic log.
If the database profiler level is 0 (i.e. database profiler is disabled), the slowms and sampleRate, affect only the diagnostic log.
(Starting in MongoDB 4.0): For a mongos instance, the method sets the slowms, sampleRate and filter configuration settings, which configure how operations get written to the diagnostic log. You cannot enable the Database Profiler on a mongos instance because mongos does not have any collections that the profiler can write to. The profile level must be 0 for a mongos instance.
Starting in MongoDB 4.4.2, you can specify a filter on both mongod and mongos instances to control which operations are logged by the profiler. When you specify a filter for the profiler, the slowms, and sampleRate options are not used for profiling and slow-query log lines.
db.setProfilingLevel() provides a wrapper around the profile command.
The db.setProfilingLevel() method has the following form:
| level | integer | Configures the database profiler level. The following profiler 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 is passed as the
|
The method returns a document that contains the previous values of the settings.
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. (New in MongoDB 4.4.2)note is a string explaining the behavior of filter. This field only appears in the output when filter is 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().
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.
The following example sets for a mongod instance:
1,20 milliseconds, and0.42.The method returns a document with the previous values for the settings.
To view the current profiling level, see db.getProfilingStatus().
The following example sets for a mongod or mongos instance:
0,20 milliseconds, and0.42.The method returns a document with the previous values for the settings.
To view the current profiling level, see db.getProfilingStatus().
New in version 4.4.2.
The following example sets for a mongod instance:
2,{ op: "query", millis: { $gt: 2000 } }, which causes the profiler to only record query operations 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().