Definition
defaultMaxTimeMSNew in version 8.0.
Available for both
mongodandmongos.Specifies a default time limit in milliseconds for individual read operations to complete. If a query specifies a
maxTimeMS()option, that value overrides thedefaultMaxTimeMSvalue.defaultMaxTimeMSapplies to the following read operations:
Access Control
To modify cluster parameters, you must authenticate as a user that has one of the following roles:
Syntax
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" } )
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() option, that value
overrides the defaultMaxTimeMS value.
Long-Running Queries
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.
Example
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" : 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:
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.