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

# Monitor Slow Queries

The [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) aggregation stage provides information on all operations currently running on MongoDB. If your application is experiencing performance issues, you can build an aggregation pipeline around this stage to monitor for slow queries and similar issues.

## About This Task

### Database Profilers

This task uses [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) to identify slow queries currently running on your application. To find all slow queries within a specified period, consider using a profiler.

| Profiler | Description |
| --- | --- |
| [Atlas Query Profiler](https://www.mongodb.com/docs/atlas/tutorial/query-profiler.md#std-label-query-profiler) | Provides a scatterplot chart to Atlas customers, making it easy to identify slow queries and performance outliers. |
| [Database Profiler](https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler.md#std-label-database-profiler) | Stores query performance information in a collection, allowing you to query MongoDB for queries with specific performance issues. |

Both the Atlas Query Profiler and the database profiler can affect server performance, use up disk space, and expose query metadata around encrypted fields. Consider the performance and security implications before enabling them.

### Explain Queries

This task identifies queries with performance issues. If you already know which queries have performance issues, see [Explain Slow Queries](https://www.mongodb.com/docs/manual/tutorial/explain-slow-queries.md#std-label-manual-explain-slow-queries) to troubleshoot them.

## Steps

1. Retrieve current operations.

   Use the [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) aggregation stage to retrieve current operations from MongoDB:

   ```javascript
   db.getSiblingDB("admin").aggregate( [
      { $currentOp: { allUsers: true } },
      { $match: { secs_running: { $gt: 2 } } },
      { $sort: { secs_running: 1 } }
   ] )
   ```

   **Output:**

   ```javascript
   [
      {
         "opid": "12345",
         "secs_running": 5,
         "active": true,
         "ns": "sample_mflix.movies",
         "command": {
            "find": "movies",
            "filter": { "title": { "$regex": "The" } }
         },
         "planSummary": "COLLSCAN",
         "locks": { ... },
         "client": "203.0.113.25:43210"
      }
   ]
   ```

   This aggregation pipeline retrieves all current operations in the cluster. The [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) aggregation stage then filters the operations to those that have been running for more than two seconds. This allows you to filter queries that run within a specified period. Adjust the value to match your application and database needs.

   The [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) stage sorts the results in ascending operation time order.

2. Check for activity.

   View the [`currentOp.active`](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-data-currentOp.active) field. If `currentOp.active` is `true`, MongoDB indicates that the operation is currently running.

   To stop a long running operation, use the [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) method to stop the given [`opid`.](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-data-currentOp.opid)

3. Check for locks.

   View the [`currentOp.waitingForLock`](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-data-currentOp.waitingForLock) field. If `currentOp.waitingForLock` is `true`, another operation running on the server or cluster is blocking the query.

   To stop a blocked operation, use the [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) method to stop the given [`opid`.](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-data-currentOp.opid)

4. Check the plan summary.

   Check the value in the [`currentOp.planSummary`](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-data-currentOp.planSummary) field.

   `IXSCAN`

   Indicates the query performed an index scan.

   `COLLSCAN`

   Indicates the query performed a full collection scan. To correct this, [Create an Index.](https://www.mongodb.com/docs/manual/core/indexes/create-index.md#std-label-manual-create-an-index)

5. Explain the query.

   If the [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) aggregation stage returns a query that requires further investigation, use the [`explain()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.explain.md#mongodb-method-db.collection.explain) method to analyze the query plan and execution statistics.

   For details, see [Explain Slow Queries.](https://www.mongodb.com/docs/manual/tutorial/explain-slow-queries.md#std-label-manual-explain-slow-queries)

## Learn More

- [Query Performance](https://www.mongodb.com/docs/manual/administration/query.md#std-label-query-performance)

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

- [Find Slow Queries](https://www.mongodb.com/docs/manual/tutorial/find-slow-queries-with-database-profiler.md#std-label-manual-find-slow-queries-with-database-profiler)

- [Explain Slow Queries](https://www.mongodb.com/docs/manual/tutorial/explain-slow-queries.md#std-label-manual-explain-slow-queries)
