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

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: standalone, repl-set, mongos
-->

# db.setProfilingLevel() (mongosh method)

## 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 method enables, disables, or configures the [Database Profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md). 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 method configures how slow operations are logged to the diagnostic log.

**Note:**

Changes made to the profiling level with `db.setProfilingLevel()` do *not* persist.  When the server restarts, it reverts to `0` (the default), or the value set by either the [`operationProfiling.mode`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.mode) setting or the [`--profile`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--profile) command-line option.

If the [database profiler level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) is `1` or `2` (specifically, the [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md) is enabled), the [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms) and [sampleRate](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-sampleRate) affect the behavior of both the profiler and the [`diagnostic log`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

If the [database profiler level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) is `0` (specifically, [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md) is disabled), the [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms) and [sampleRate](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-sampleRate), affect only the diagnostic log.

With [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances, 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](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md) 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. The `profile` level must be `0` for a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance.

You can specify a [filter](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter) on both [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) and [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances to control which operations are logged by the profiler. When you specify a `filter` for the profiler, the [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms), and [sampleRate](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-sampleRate) options are not used for profiling and slow-query log lines.

[`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) provides a wrapper around the [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command.

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)

## Compatibility

This method 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

**Important:**

This command is not supported in M0 and Flex clusters. For more information, see [Unsupported Commands.](https://www.mongodb.com/docs/atlas/unsupported-commands/)

- [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 [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) method has the following form:

```javascript
db.setProfilingLevel(<level>, <options>)
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| [level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) | integer | Configures the [database profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md) 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`. Because profiling is not available on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) 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. |
| [options](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options) | document or integer | Optional. Accepts an integer or an options document. If an integer value is passed as the `options` argument instead of a document, the value is assigned to [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms). The following options are available: slowms:Default: 100Type: integerThe 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.For [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instances, the setting affects both the diagnostic log and, if enabled, the profiler.For [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on [`mongos`.](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos)This argument affects the same setting as the configuration file option [`slowOpThresholdMs`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpThresholdMs); slowinprogms:**New in version 8.3**Default: 5000Type: integerThe slow operation time threshold for an in-progress query, in milliseconds. MongoDB logs operations that run for longer than this threshold as slow in-progess queries. MongoDB logs a query as a slow in-progress query as soon as the query operation crosses the time threshold.This argument affects the same setting as the configuration file option [`slowOpInProgressThresholdMs`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpInProgressThresholdMs); sampleRate:Default: 1.0Type: doubleThe fraction of *slow* operations to profile or log. `sampleRate` accepts values between 0 and 1, inclusive.; filter:Type: objectA filter expression that controls which operations are profiled and logged. The field in the filter expression can be [any field in the profiler output.](https://www.mongodb.com/docs/manual/reference/database-profiler.md#std-label-profiler)For [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instances, the setting affects both the diagnostic log and, if enabled, the profiler.For [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances, the setting affects the diagnostic log only and not the profiler because profiling is not available on [`mongos`.](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos)Starting in MongoDB 8.0, you can specify `workingMillis` as a filter parameter to log operations based on the amount of time MongoDB spends working on that operation.For an example of a filter used to control logged operations, see [Set a Filter to Determine Profiled Operations.](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-profiler-filter-example)When a profiling [filter](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter) is set, the [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms) and [sampleRate](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-sampleRate) options do not affect the diagnostic log or the profiler. |

## Returns

The method returns a document that contains the **previous** values of the settings.

### Standalone

```javascript
{
   "was" : 2,
   "slowms" : 100,
   "sampleRate" : 1,
   "filter" : {
      "$and" : [
         {
            "op" : {
               "$eq" : "query"
            }
         },
         {
            "millis" : {
               "$gt" : 20000
            }
         }
      ]
   },
   "note" : "When a filter expression is set, slowms and sampleRate are not used for profiling and slow-query log lines.",
   "ok" : 1
}
```

Where:

- `was` is the **previous** [level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) setting.

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

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

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

- `note` is a string explaining the behavior of `filter`. This field only appears in the output when `filter` is also present.

**Note:**

The `filter` and `note` fields only appear in the output if they were present in the previous [level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) setting.

To view the current profiling level, see [`db.getProfilingStatus()`.](https://www.mongodb.com/docs/manual/reference/method/db.getprofilingstatus.md#mongodb-method-db.getProfilingStatus)

## Behavior

**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.

## Examples

### Enable Profiler and Set Slow Operation Threshold and Sample Rate

The following example sets for a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance:

- the [profiling level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) to `1`,

- the slow operation threshold [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms) to `20` milliseconds, and

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

```javascript
db.setProfilingLevel(1, { slowms: 20, sampleRate: 0.42 })
```

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)

### Disable Profiler and Set Slow Operation Threshold and Sample Rate

The following example sets for a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) or [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance:

- the [profiling level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) to `0`,

- the slow operation threshold [slowms](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-slowms) to `20` milliseconds, and

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

```javascript
db.setProfilingLevel(0, { slowms: 20, sampleRate: 0.42 })
```

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)

### Set a Filter to Determine Profiled Operations

The following example sets for a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance:

- the [profiling level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) to `1`,

- a [filter](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter) of `{ op: "query", millis: { $gt: 2000 } }`, which causes the profiler to only record `query` operations that took longer than 2 seconds.

```javascript
db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )
```

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)

### Set a Filter Without Enabling the Profiler

You can set a `filter` without enabling the database profiler. The following example uses a filter with a [profiling level](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-level) of `0`.

```javascript
db.setProfilingLevel( 0, { filter: { op: "query", millis: { $gt: 2000 } } } )
```

Because the profiling level is `0`, the profiler is disabled and does not record matching operations to the profiler collection. The filter still applies to the diagnostic log.

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)

### Unset a Filter

To clear a profile filter, run `db.setProfilingLevel()` with the `filter: "unset"` option.

```javascript
db.setProfilingLevel( 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)
