> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# profile (database command)

## Definition

**Changed in version 5.0**

For a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance, the command enables, disables, or configures the [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler). The profiler captures and records data on the performance of write operations, cursors, and database commands on a running [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance. If the profiler is disabled, the command configures how slow operations are logged to the diagnostic log.

On [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod), if the [database profiler level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) is `2`, full logging is enabled on the profiler and the [`diagnostic log`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

At [database profiler level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) `1`, the following settings modify both the profiler and the [`diagnostic log`:](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

- [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms)

- [sampleRate](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-sampleRate)

- [filter](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter)

If the [database profiler level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) is `0`, the [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler) is disabled. At level `0` the following settings only modify the diagnostic log:

- [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms)

- [sampleRate](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-sampleRate)

- [filter](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter)

For a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance, the command only configures how operations get written to the diagnostic log. You cannot enable the [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler) on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance because `mongos` does not have any collections that the profiler can write to.

Starting in MongoDB 5.0, changes made to the [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler) `level`, `slowms`, `sampleRate`, or `filter` using the [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command or [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) wrapper method are recorded in the [`log file`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

On [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), you can set `profile` level to:

- `0` to set the `slowms`, `sampleRate`, and `filter` 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](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-overhead) for more information on potential performance degradation.

## Compatibility

This command is available in deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

**Note:**

Atlas Infinite clusters only support this command when the [database profiler level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) is `0`.

Atlas Core clusters support all [database profiler levels.](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level)

- [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

- [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The command has the following syntax:

```javascript
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: `0`The profiler is off and does not collect any data. This is the default profiler level.`1`The profiler collects data for operations that exceed the `slowms` threshold or match a specified [filter.](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter)When a filter is set:The `slowms` and `sampleRate` options are not used for profiling.; The profiler only captures operations that match the [filter.](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter)`2`The profiler collects data for all operations.When set to level `2`, the profiler ignores user provided values for `slowms` and `filter`. Since profiling is not available on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), the [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command cannot be used to set the profiling level to a value other than `0` on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance. |
| `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 `workingMillis`, which is the amount of time that MongoDB spends working on that operation. This means that factors such as waiting for locks and flow control do not affect whether an operation exceeds the slow operation threshold. When [`logLevel`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.logLevel) is set to `0`, MongoDB records *slow* operations to the diagnostic log at a rate determined by [`slowOpSampleRate`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpSampleRate) At higher `logLevel` settings, 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 the `logLevel` does not log all oplog entries. This argument affects the same setting as the configuration option [`operationProfiling.slowOpThresholdMs`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpThresholdMs) |
| `sampleRate` | double | Optional. Default: 1.0 The fraction of *slow* operations that should be profiled or logged. `sampleRate` accepts values between 0 and 1, inclusive. This argument affects the same setting as the configuration option [`operationProfiling.slowOpSampleRate`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpSampleRate) and does not affect the slow oplog entry log messages on secondaries. |
| `filter` | object | Optional. A query that determines which operations are profiled or logged. The filter query takes the following form: `{ <field1>: <expression1>, ... }` The query can be any legal [`find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) operation where the query `<field>` matches a field in the [profiler output.](https://www.mongodb.com/docs/manual/reference/database-profiler.md#std-label-profiler) This argument affects the same setting as the configuration option [`operationProfiling.filter`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.filter). When `filter` is set, the `slowms` and `sampleRate` options are not used for profiling and slow-query log lines. |

The [`db.getProfilingStatus()`](https://www.mongodb.com/docs/manual/reference/method/db.getprofilingstatus.md#mongodb-method-db.getProfilingStatus) and [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) [shell methods](https://www.mongodb.com/docs/manual/reference/method.md#std-label-js-administrative-methods) provide wrappers around the [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command.

## Behavior

The [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.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`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command has completed.

When connected to a sharded cluster through [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), you can run the [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command against any database.

**See also:**

[Database Profiling](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling)

## Example

### Enable Filtering

To enable profiling and filter the logged data:

```javascript
db.runCommand(
   {
     profile: 1,
     filter:
        {
           $or:
           [
              { millis: { $gte: 100 } },
              { user: "testuser@admin" }
           ]
        }
   }
)
```

The [filter](https://www.mongodb.com/docs/manual/reference/command/profile.md#std-label-profile-filter-def) only selects operations that are:

- at least `100` milliseconds long, or

- submitted by the `testuser`.

### Unset a Filter

To clear a profile filter, run `profile` with the `filter: "unset"` option.

```javascript
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()`.](https://www.mongodb.com/docs/manual/reference/method/db.getprofilingstatus.md#mongodb-method-db.getProfilingStatus)
