Docs Menu
Docs Home
/
MongoDB Manual
/ / / /

defaultMaxTimeMS

On this page

  • Definition
  • Access Control
  • Syntax
  • Behavior
  • Long-Running Queries
  • Example
  • Results
defaultMaxTimeMS

New in version 8.0.

Available for both mongod and mongos.

Specifies a default time limit in milliseconds for individual read operations to complete. If a query specifies a maxTimeMS() option, that value overrides the defaultMaxTimeMS value.

defaultMaxTimeMS applies to the following read operations:

To modify cluster parameters, you must authenticate as a user that has one of the following roles:

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

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

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

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

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() option, that value overrides the defaultMaxTimeMS value.

If your deployment needs to run long queries, such as analytics node queries, you must specify a timeout for those queries at the operation level using 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.

The following command sets the default query timeout 5000 milliseconds:

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

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

db.adminCommand( { getClusterParameter: "defaultMaxTimeMS" } )
{
"clusterParameters" : [
{
"_id" : "defaultMaxTimeMS",
"clusterParameterTime" : Timestamp(1711564868, 17),
"readOperations" : NumberLong(5000)
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1712161244, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1712161244, 1)
}

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

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(), which overrides the defaultMaxTimeMS and causes the query to timeout after 8,000 milliseconds.

Back

changeStreamOptions