Terminate Running Operations


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


New in version 2.6.

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:

db.location.find( { "town": { "$regex": "(Pine Lumber)",
                              "$options": 'i' } } ).maxTimeMS(30)

Terminate a Command

Consider a potentially long running operation using distinct to return each distinct``collection`` field that has a city key:

db.runCommand( { distinct: "collection",
                 key: "city" } )

You can add the maxTimeMS field to the command document to set a time limit of 30 milliseconds for the operation:

db.runCommand( { distinct: "collection",
                 key: "city",
                 maxTimeMS: 45 } )

db.getLastError() and db.getLastErrorObj() will return errors for interrupted options:

{ "n" : 0,
  "connectionId" : 1,
  "err" : "operation exceeded time limit",
  "ok" : 1 }


The db.killOp() method interrupts a running operation at the next interrupt point. db.killOp() identifies the target operation by operation ID.