Definition
profileChanged in version 4.4.2.
For a
mongodinstance, the command 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 command configures how slow operations are logged to the diagnostic log.On
mongod, if the database profiler level is1or2(i.e. the database profiler is enabled), the slowms, sampleRate, and filter affect the behavior of both the profiler and thediagnostic log.If the database profiler level is
0(i.e. database profiler is disabled), the slowms, sampleRate, and filter affect only the diagnostic log.Starting in MongoDB 4.0, for a
mongosinstance, the command only configures 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.Starting in MongoDB 4.4.2 (also available starting in 4.2.12), changes made to the database profiler
level,slowms,sampleRate, orfilterusing theprofilecommand ordb.setProfilingLevel()wrapper method are recorded in thelog file.On
mongos, you can setprofilelevel to:0to set theslowms,sampleRate, andfilterfor the diagnostic log;-1to read the current settings.
The profiler is off by default.
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.
The
profilecommand has the following syntax:{ profile: <level>, slowms: <threshold>, sampleRate: <rate>, filter: <filter expression> } FieldTypeDescriptionprofileint
Configures the profiler level. The following profiler levels are available:
LevelDescription0The profiler is off and does not collect any data. This is the default profiler level.
1The profiler collects data for operations that take longer than the value of
slowms.2The profiler collects data for all operations.
Since profiling is not available on
mongos, theprofilecommand cannot be used to set the profiling level to a value other than0on amongosinstance.slowmsint
Optional. Default: 100
The slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow.
When
logLevelis set to0, MongoDB records slow operations to the diagnostic log at a rate determined byslowOpSampleRate.At higher
logLevelsettings, all operations appear in the diagnostic log regardless of their latency with the following exception: the logging of slow oplog entry messages by the secondaries. The secondaries log only the slow oplog entries; increasing thelogLeveldoes not log all oplog entries.Note
This argument affects the same setting as the configuration option
slowOpThresholdMs.sampleRatedouble
Optional. Default: 1.0
The fraction of slow operations that should be profiled or logged.sampleRateaccepts values between 0 and 1, inclusive.Note
This argument affects the same setting as the configuration option
slowOpSampleRateand does not affect the slow oplog entry log messages on secondaries (available starting in MongoDB 4.2).New in version 3.6.
filterobject
Optional.
A filter expression that controls which operations are profiled and logged.
The
filterexpression takes the following form:{ <field1>: <expression1>, ... } The
<field>can be any field in the profiler output. The<expression>is a query condition expression.Note
This argument affects the same setting as the configuration option
filter. Whenfilteris set, theslowmsandsampleRateoptions are not used for profiling and slow-query log lines.New in version 4.4.2.
The db.getProfilingStatus() and
db.setProfilingLevel() shell methods provide wrappers around the profile
command.
Behavior
The profile command obtains a write lock on the affected
database while enabling or disabling the profiler. This is typically a
short operation. The lock blocks other operations until the
profile command has completed.
Starting in MongoDB 4.4.2, when connected to a sharded cluster through
mongos, you can run the profile command
against any database. In previous versions of MongoDB, when connected
through mongos, you can only run the profile
command against the admin database.