profile
On this page
Definition
profile
Changed in version 5.0.
For a
mongod
instance, 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 runningmongod
instance. If the profiler is disabled, the command configures how slow operations are logged to the diagnostic log.On
mongod
, if the database profiler level is2
, full logging is enabled on the profiler and thediagnostic log
.At database profiler level
1
, the following settings modify both the profiler and thediagnostic log
:If the database profiler level is
0
, the database profiler is disabled. At level0
the following settings only modify the diagnostic log:For a
mongos
instance, the command only configures 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.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
.On
mongos
, you can setprofile
level to:0
to set theslowms
,sampleRate
, andfilter
for the diagnostic log;-1
to 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.
Compatibility
This command is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Note
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, 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 command has the following syntax:
db.runCommand( { profile: <level>, slowms: <threshold>, sampleRate: <rate>, filter: <filter expression> } )
Command Fields
The command takes the following fields:
Field | Type | Description | |
---|---|---|---|
profile | int | Configures the profiler level. The following profiler levels are available:
Since profiling is not available on | |
slowms | int | Optional. Default: 100 The slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow. Slow operations are logged based on When At higher This argument affects the same setting as the configuration option
| |
sampleRate | double | Optional. Default: 1.0 The fraction of slow operations that should be profiled or logged.
This argument affects the same setting as the configuration option
| |
filter | object | Optional. A query that determines which operations are profiled or logged. The filter query takes the following form:
The query can be any legal This argument affects the same setting as the
configuration option |
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.
When connected to a sharded cluster through mongos
, you can run
the profile
command against any database.
Example
Enable Filtering
To enable profiling and filter the logged data:
db.runCommand( { profile: 1, filter: { $or: [ { millis: { $gte: 100 } }, { user: "testuser@admin" } ] } } )
The filter only selects operations that are:
at least
100
milliseconds long, orsubmitted by the
testuser
.
Unset a Filter
To clear a profile filter, run profile
with the filter: "unset"
option.
db.runCommand( { profile: 1, filter: "unset" } )
The operation returns a document with the previous values for the settings.
To view the current profiling level, see
db.getProfilingStatus()
.