- Administration >
- Configuration and Maintenance >
- Terminate Running Operations
Terminate Running Operations¶
On this page
Overview¶
MongoDB provides two facilitates to terminate running operations:
maxTimeMS()
and db.killOp()
. Use these
operations as needed to control the behavior of operations in a
MongoDB deployment.
Available Procedures¶
maxTimeMS
¶
The maxTimeMS()
method sets a time limit for an
operation. When the operation reaches the specified time limit,
MongoDB interrupts the operation at the next interrupt point.
Terminate a Query¶
From the mongo
shell, use the following method to set a
time limit of 30 milliseconds for this query:
Terminate a Command¶
Consider a potentially long running operation using
distinct
to return each distinct collection
field that
has a city
key:
You can add the maxTimeMS
field to the command document to set a
time limit of 45 milliseconds for the operation:
db.getLastError()
and db.getLastErrorObj()
will return
errors for interrupted options:
killOp
¶
The db.killOp()
method interrupts a running operation at
the next interrupt point. db.killOp()
identifies
the target operation by operation ID.
Warning
Terminate running operations with extreme caution. Only use
db.killOp()
to terminate operations initiated by clients
and do not terminate internal database operations.
Sharded Cluster¶
Starting in MongoDB 4.0, the killOp
command can be run on
a mongos
and can kill queries (i.e. read operations)
that span shards in a cluster. The killOp
command from the
mongos
does not propagate to the shards when the
operation to be killed is a write operation.
For more information on killing operations on a sharded cluster, see:
For information on how to list sharding operations that are active on a
mongos
, see the localOps
parameter in
$currentOp
.