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

# defaultMaxTimeMS

## Definition

**New in version 8.0**

Atlas clusters and self-hosted deployments

Specifies a cluster-wide default time limit in milliseconds for each individual read operation to complete. If a query specifies a [`maxTimeMS()`](https://www.mongodb.com/docs/manual/reference/method/cursor.maxtimems.md#mongodb-method-cursor.maxTimeMS) option, that value overrides the `defaultMaxTimeMS` value.

`defaultMaxTimeMS` applies to the following read operations:

- [`aggregate`](https://www.mongodb.com/docs/manual/reference/command/aggregate.md#mongodb-dbcommand-dbcmd.aggregate) (except [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) and [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out) stages)

- [`count`](https://www.mongodb.com/docs/manual/reference/command/count.md#mongodb-dbcommand-dbcmd.count)

- [`dbHash`](https://www.mongodb.com/docs/manual/reference/command/dbhash.md#mongodb-dbcommand-dbcmd.dbHash)

- [`distinct`](https://www.mongodb.com/docs/manual/reference/command/distinct.md#mongodb-dbcommand-dbcmd.distinct)

- [`find`](https://www.mongodb.com/docs/manual/reference/command/find.md#mongodb-dbcommand-dbcmd.find)

## Access Control

To modify cluster parameters, you must [authenticate](https://www.mongodb.com/docs/manual/core/authentication.md#std-label-authentication) as a user that has one of the following roles:

- [`clusterAdmin`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-clusterAdmin)

- [`clusterMonitor`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-clusterMonitor)

## Syntax

To set `defaultMaxTimeMS` for your deployment, run the following command on the `admin` database:

```javascript
db.adminCommand(
   {
      setClusterParameter: {
         defaultMaxTimeMS: { readOperations: <value> }
      }
   }
)
```

To view the current value for `defaultMaxTimeMS`, run the following command on the `admin` database:

```javascript
db.adminCommand( { getClusterParameter: "defaultMaxTimeMS" } )
```

## Behavior

By default, `defaultMaxTimeMS.readOperations` is 0, meaning no default query timeout is set. If there is no default query timeout, the query runs until it either returns a result or fails.

If a query specifies a [`maxTimeMS()`](https://www.mongodb.com/docs/manual/reference/method/cursor.maxtimems.md#mongodb-method-cursor.maxTimeMS) option, that value overrides the `defaultMaxTimeMS` value.

**Note:**

`defaultMaxTimeMS` applies only to read operations.

Aggregations that include the [`$out`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/out.md#mongodb-pipeline-pipe.-out) or [`$merge`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#mongodb-pipeline-pipe.-merge) stages perform write operations, so they are not subject to `defaultMaxTimeMS`. A [`maxTimeMS()`](https://www.mongodb.com/docs/manual/reference/method/cursor.maxtimems.md#mongodb-method-cursor.maxTimeMS) value still applies to those aggregations.

### Long-Running Queries

If your deployment needs to run long queries, such as [analytics node](https://www.mongodb.com/docs/atlas/reference/faq/deployment.md#std-label-analytics-nodes-overview) queries, you must specify a timeout for those queries at the operation level using [`maxTimeMS()`](https://www.mongodb.com/docs/manual/reference/method/cursor.maxtimems.md#mongodb-method-cursor.maxTimeMS). If you don't specify an operation timeout, those queries use the `defaultMaxTimeMS` timeout, and won't run for the required amount of time.

## Example

The following command sets the default query timeout `5000` milliseconds:

```javascript
db.runCommand( {
   setClusterParameter: {
      defaultMaxTimeMS: { readOperations: 5000 }
   }
} )
```

To check the value of `defaultMaxTimeMS`, run the following command:

```javascript
db.adminCommand( { getClusterParameter: "defaultMaxTimeMS" } )
```

**Output:**

```javascript
{
   "clusterParameters" : [
      {
         "_id" : "defaultMaxTimeMS",
         "clusterParameterTime" : Timestamp(1711564868, 17),
         "readOperations" : Long(5000)
      }
   ],
   "ok" : 1,
   "$clusterTime" : {
      "clusterTime" : Timestamp(1712161244, 1),
      "signature" : {
         "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
         "keyId" : Long(0)
      }
   },
   "operationTime" : Timestamp(1712161244, 1)
}
```

### Results

After you set `defaultMaxTimeMS` for your deployment, consider these queries:

```javascript
db.test.find( { name: "Carol" } )

db.test.find( { name: "Carol" } ).maxTimeMS( 8000 )
```

The first query uses the `defaultMaxTimeMS` value of 5,000 milliseconds.

The second query specifies [`maxTimeMS()`](https://www.mongodb.com/docs/manual/reference/method/cursor.maxtimems.md#mongodb-method-cursor.maxTimeMS), which overrides the `defaultMaxTimeMS` and causes the query to timeout after 8,000 milliseconds.
