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

# Database Profiler

**Warning:**

The database profiler can degrade MongoDB performance. Before enabling the database profiler, consider using one of the following alternatives:

- [Atlas Query Profiler](https://www.mongodb.com/docs/atlas/tutorial/query-profiler.md#std-label-profile-database)

- [Atlas Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor.md#std-label-performance-advisor)

- [`$queryStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats.md#mongodb-pipeline-pipe.-queryStats) (aggregation stage)

To learn more about the performance impact of the database profiler, see [Profiler Overhead.](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-overhead)

The database profiler collects detailed information about [Database Commands](https://www.mongodb.com/docs/manual/reference/command.md#std-label-database-commands) executed against a running [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance. This includes CRUD operations as well as configuration and administration commands.

The profiler writes all the data it collects to a [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection, a [capped collection](https://www.mongodb.com/docs/manual/core/capped-collections.md#std-label-manual-capped-collection) in each profiled database. See [Database Profiler Output](https://www.mongodb.com/docs/manual/reference/database-profiler.md) for an overview of the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) documents created by the profiler.

The profiler is `off` by default. You can enable the profiler on a per-database or per-instance basis at one of several [profiling levels.](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-level)

When enabled, profiling has an effect on database performance and disk use. See [Database Profiler Overhead](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-overhead) for more information.

This page shows important administration options for the database profiler. For additional information, see:

- [Database Profiler Output](https://www.mongodb.com/docs/manual/reference/database-profiler.md#std-label-profiler)

- [Profile Command](https://www.mongodb.com/docs/manual/reference/command/profile.md#std-label-profile-command)

- [`db.currentOp()`](https://www.mongodb.com/docs/manual/reference/method/db.currentop.md#mongodb-method-db.currentOp)

## Profiling Levels

The following profiling 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`.

## Enable and Configure Database Profiling

You can enable database profiling for [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instances.

To enable profiling, use one of the following methods:

- To enable profiling at startup, set [`operationProfiling.mode`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.mode) in your [configuration file.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options)

- To enable profiling during runtime, use the [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) helper method [`db.setProfilingLevel()`.](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel)

- To enable profiling with a driver, use a [driver method.](https://www.mongodb.com/docs/drivers/)

The profiler records data in the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection. MongoDB creates the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection in a database after you enable profiling for that database.

To enable profiling for a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance at startup, set [`operationProfiling.mode`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.mode) in your [configuration file](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options) to your preferred logging level.

To enable profiling and set the profiling level, pass the profiling level to the [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) helper. For example, to enable profiling for all database operations for the currently connected database, run this operation in [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```javascript
db.setProfilingLevel(2)
```

The shell returns the *previous* profiling level in the `was` field and sets the new level. In the following output, the `"ok" :
1` key-value pair indicates the operation succeeded:

```javascript
{ "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }
```

To verify the new setting, see the [Check Profiling Level](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-view-status) section.

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)

### Global and Per-Database Profiling Settings

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) profiling settings are *global*. When set, these settings affect all databases in your process.

When set through 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) shell helper method, [profiling level](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-level) and [filter](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#std-label-set-profiling-level-options-filter) settings are set at the *database* level. When set as either command line or [configuration file](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options) options, profiling level and `filter` settings affect the entire process.

### Specify the Threshold for Slow In-Progress Operations

Starting in MongoDB 8.3, you can set a slow in-progress operation threshold. By default, the slow in-progress operation threshold is 5000 milliseconds. You can set the threshold with [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) or the [`mongod --defaultSlowInProgMS`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--defaultSlowInProgMS) command line option.

### Specify the Threshold for Slow Operations

By default, the slow operation threshold is 100 milliseconds.

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.

To change the slow operation threshold, specify the required threshold value in one of the following ways:

- Set the value of `slowms` 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) shell helper method.

- Set the value of [`--slowms`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--slowms) from the command line at startup.

- Set the value of [`slowOpThresholdMs`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpThresholdMs) in a [configuration file.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options)

The following example sets the profiling level for the currently connected database to `1` and sets the slow operation threshold for the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance to `20` milliseconds:

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

A profiling level of `1` causes the profiler to record operations slower than the `slowms` threshold.

**Important:**

The slow operation threshold applies to all databases in a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance. It is used by both the database profiler and the diagnostic log and should be set to the highest useful value to avoid performance degradation.

You can use [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) to configure `slowms` and `sampleRate` for [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos). For the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), the `slowms` and `sampleRate` configuration settings only affect the diagnostic log and not the profiler since profiling is not available on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos).&#x20;

The following example sets a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance's slow operation threshold for logging slow operations to `20`:

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

The [profiler entries](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler) and the [diagnostic log messages (i.e. mongod/mongos logmessages)](https://www.mongodb.com/docs/manual/reference/log-messages.md#std-label-log-message-slow-ops) for read/write operations include:

- `planCacheShapeHash` to help identify slow queries with the same [plan cache query shape.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-plan-cache-query-shape)

  Starting in MongoDB 8.0, the existing `queryHash` field is duplicated in a new field named `planCacheShapeHash`. If you're using an earlier MongoDB version, you'll only see the `queryHash` field. Future MongoDB versions will remove the deprecated `queryHash` field, and you'll need to use the `planCacheShapeHash` field instead.

- `planCacheKey` to provide more insight into the [query plan cache](https://www.mongodb.com/docs/manual/core/query-plans.md) for slow queries.

Secondary members of a replica set now [log oplog entries](https://www.mongodb.com/docs/manual/core/replica-set-oplog.md#std-label-slow-oplog-application) that take longer than the slow operation threshold to apply. These slow oplog messages:

- Are logged for the secondaries in the [`diagnostic log`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

- Are logged under the [`REPL`](https://www.mongodb.com/docs/manual/reference/log-messages.md#mongodb-data-REPL) component with the text `applied op: <oplog entry> took <num>ms`.

- Do not depend on the log levels (either at the system or component level)

- Do not depend on the profiling level.

- Are affected by [`slowOpSampleRate`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpSampleRate)

The profiler does not capture slow oplog entries.

### Profile a Random Sample of Slow Operations

To profile only a randomly sampled subset of all *slow* operations, specify the desired sample rate in one of the following ways:&#x20;

- Set the value of `sampleRate` 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) shell helper method.

- Set the value of [`--slowOpSampleRate`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--slowOpSampleRate) for [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) or [`--slowOpSampleRate`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-option-mongos.--slowOpSampleRate) for [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) from the command line at startup.

- Set the value of [`slowOpSampleRate`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpSampleRate) in a [configuration file.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options)

By default, `sampleRate` is set to `1.0`, meaning all *slow* operations are profiled. When `sampleRate` is set between `0` and `1`, databases with a profiling level `1` only profile a randomly sampled percentage of *slow* operations based on `sampleRate`.

The following example sets the profiling level for the currently connected database to `1` and sets the profiler to sample 42% of all *slow* operations:

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

The modified sample rate value also applies to the system log.

You can use [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) to configure `slowms` and `sampleRate` for [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos). For the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos),  the `slowms` and `sampleRate` configuration settings only affect the diagnostic log and not the profiler because profiling isn't available on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos).&#x20;

For example, the following sets a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance's sampling rate for logging slow operations:

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

**Important:**

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.

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

### Set a Filter to Determine Profiled Operations

You can set a filter to control which operations are profiled and logged. You can set the profiling filter in one of the following ways:

- Set the value of `filter` using the [`profile`](https://www.mongodb.com/docs/manual/reference/command/profile.md#mongodb-dbcommand-dbcmd.profile) command or the [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) shell helper method.

- Set the value of [`filter`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.filter) in a [configuration file.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options)

For [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instances, the `filter` 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 `filter` affects the diagnostic log only and not the profiler since profiling is not available on [`mongos`.](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos)

**Note:**

When a profiling `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.

The following [`db.setProfilingLevel()`](https://www.mongodb.com/docs/manual/reference/method/db.setprofilinglevel.md#mongodb-method-db.setProfilingLevel) example sets the profile level for the currently connected database:

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

- the [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 log `query` operations that took longer than 2 seconds.

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

### Check Profiling Level

To view the [profiling level](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-levels), run the following example in [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```javascript
db.getProfilingStatus()
```

The shell returns a document similar to the following:

```javascript
{ "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }
```

The `was` field indicates the current profiling level.

The `slowms` field indicates operation time threshold, in milliseconds, beyond which operations are considered *slow*.

The `sampleRate` field indicates the percentage of *slow* operations that should be profiled.

### Disable Profiling

To disable profiling, run the following example in [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```javascript
db.setProfilingLevel(0)
```

**Note:**

Disabling profiling can improve database performance and lower disk use. For more information, see [Database Profiler Overhead](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-overhead)  .

### Enable Profiling for an Entire `mongod` Instance

For development and test environments, you can enable database profiling for an entire [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance. The profiling level applies to all databases provided by the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance.

To enable profiling for a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance, pass the following options to [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) at startup.

```bash
mongod --profile 1 --slowms 15 --slowOpSampleRate 0.5
```

Alternatively, you can specify [operationProfiling](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-operation-profiling-configuration-options) in the [configuration file.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#std-label-configuration-options)

This sets the profiling level to `1`, defines slow operations as those that last longer than `15` milliseconds, and specifies that only 50% of *slow* operations should be profiled.&#x20;

The `slowms` and `slowOpSampleRate` also affect the operations that are recorded in the diagnostic log when [`logLevel`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.logLevel) is set to `0`. The `slowms` and `slowOpSampleRate` are also available to configure diagnostic logging for [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos).&#x20;

**See also:**

- [`mode`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.mode)

- [`slowOpThresholdMs`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpThresholdMs)

- [`slowOpInProgressThresholdMs`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpInProgressThresholdMs)

- [`slowOpSampleRate`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpSampleRate)

### Database Profiling and Sharding

You *cannot* enable profiling on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance. To enable profiling in a sharded cluster, you must enable profiling for each [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instance in the cluster.

However, you can set the [`--slowms`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-option-mongos.--slowms) and [`slowOpSampleRate`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#std-option-mongos.--slowOpSampleRate) on [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) to configure the diagnostic log for slow operations.

## View Profiler Data

The database profiler logs information about database operations in the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection.

To view profiling information, query the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection. To view example queries, see [Example Profiler Data Queries](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-example-queries). For an explanation of the output data, see [Database Profiler Output.](https://www.mongodb.com/docs/manual/reference/database-profiler.md)

It is no longer possible to perform any operation, including reads, on the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection from within a [transaction.](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions)

### Example Profiler Data Queries

This section shows example queries on the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection. For query output details, see [Database Profiler Output.](https://www.mongodb.com/docs/manual/reference/database-profiler.md)

To return the most recent 10 log entries in the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection, run a query similar to the following:

```javascript
db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()
```

To return all operations except command operations ([$cmd](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term--cmd)), run a query similar to the following:

```javascript
db.system.profile.find( { op: { $ne : 'command' } } ).pretty()
```

To return operations for a particular collection, run a query similar to the following. This example returns operations in the `mydb` database's `test` collection:

```javascript
db.system.profile.find( { ns : 'mydb.test' } ).pretty()
```

To return operations that take longer than 5 milliseconds to complete, run:

```javascript
db.system.profile.find( { millis : { $gt : 5 } } ).pretty()
```

To return operations for a specific time range, run:

```javascript
db.system.profile.find( {
   ts : {
      $gt: new ISODate("2012-12-09T03:00:00Z"),
      $lt: new ISODate("2012-12-09T03:40:00Z")
   }
} ).pretty()
```

The following example looks at the time range, suppresses the `user` field from the output to make it easier to read, and sorts the results by how long each operation took to run:

```javascript
db.system.profile.find( {
   ts : {
      $gt: new ISODate("2011-07-12T03:00:00Z"),
      $lt: new ISODate("2011-07-12T03:40:00Z")
   }
}, { user: 0 } ).sort( { millis: -1 } )
```

### Show the Five Most Recent Events

On a database that has profiling enabled, the `show profile` helper in [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) displays the 5 most recent operations that took at least 1 millisecond to execute. Run `show profile` from [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```javascript
show profile
```

## Profiler Overhead

When enabled, profiling has an effect on database performance, especially when configured with a [profiling level](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-level) of 2, or when using a low [threshold](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiling-specify-slowms-threshold) value with a profiling level of 1.

Profiling also uses disk space, because profiling writes logs to the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection and the MongoDB [`logfile`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

**Warning:**

Consider performance and storage implications before you enable the profiler in a production deployment.

### The `system.profile` Collection

The [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection is a [capped collection](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-capped-collection) with a default size of 1 megabyte. A collection of this size can typically store several thousand profile documents, but some applications may use more or less profiling data per operation. If you need to change the size of the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection, follow the steps below.

### Change Size of `system.profile` Collection on the Primary

To change the size of the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection on the [primary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary), you must:

1. Disable profiling.

2. Drop the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection.

3. Create a new [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection.

4. Re-enable profiling.

For example, to create a new [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection that is `4000000` bytes (4 MB), use the following sequence of operations in [`mongosh`:](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

```javascript
db.setProfilingLevel(0)

db.system.profile.drop()

db.createCollection( "system.profile", { capped: true, size:4000000 } )

db.setProfilingLevel(1)
```

### Change Size of `system.profile` Collection on a Secondary

To change the size of the [`system.profile`](https://www.mongodb.com/docs/manual/reference/system-collections.md#mongodb-data--database-.system.profile) collection on a [secondary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-secondary), you must stop the secondary, run it as a standalone, and then perform the steps above. When done, restart the standalone as a member of the replica set. For more information, see [Perform Maintenance on Self-Managed Replica Set Members.](https://www.mongodb.com/docs/manual/tutorial/perform-maintenance-on-replica-set-members.md#std-label-perform-maint-on-replica-set)

Secondary members of a replica set now [log oplog entries](https://www.mongodb.com/docs/manual/core/replica-set-oplog.md#std-label-slow-oplog-application) that take longer than the slow operation threshold to apply. These slow oplog messages:

- Are logged for the secondaries in the [`diagnostic log`.](https://www.mongodb.com/docs/manual/reference/program/mongod.md#std-option-mongod.--logpath)

- Are logged under the [`REPL`](https://www.mongodb.com/docs/manual/reference/log-messages.md#mongodb-data-REPL) component with the text `applied op: <oplog entry> took <num>ms`.

- Do not depend on the log levels (either at the system or component level)

- Do not depend on the profiling level.

- Are affected by [`slowOpSampleRate`.](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-operationProfiling.slowOpSampleRate)

The profiler does not capture slow oplog entries.
