Managing Long-Running Operations in MongoDB

HManaging Long-Running Operations in MongoDBMongoDB gives you a number of tools to manage long-running operations in the system. It’s extremely important to keep track of operations running on your production server at any point of time, as in some cases, you might have rouge queries or index builds that are killing the performance of your server.

The MongoDB command which gives you this information is “db.currentOp()”. For more information refer to the MongoDB documentation for db.currentOp(). The command takes parameters to restrict the output by either db, operation, execution time etc.

Here is an example output:

  opid: 294,
  active: false,
  op: "query",
  ns: "admin",
  query: { "query": {}, orderby: { "$natural": -1 } },
  client: "",
  desc: "rsMgr",
  threadId: "0x7f3e6af3f700",
  waitingForLock: false,
  numYields: 0,
  lockStats: { timeLockedMicros: { R: 1, W: 1 },
  timeAcquiringMicros: { r: 1, w: 1 }

The most interesting fields are:

  1. opid - ID of the operation.
  2. op - The operation that is executing.
  3. ns - The database and collection the operation is executing on.
  4. secs_running - The number of seconds the operation has been running.

Once you identify the long-running operations, you might want to terminate them in some cases. The operation you want to use is:


It goes without saying that you want to use this command very carefully. Do not terminate operations you do not know about. I personally only feel comfortable terminating long-running queries.

If you have operations that are consistently taking a long time, MongoDB provides another option – maxTimeMS:

E.g. db.find(...).maxTimeMS(30)

If you know you have long-running operations that you want to stop after a certain amount of execution time has exceeded, use the maxTimeMS option to set a limit on the execution time of this op.

At ScaleGrid, we understand the importance of this workflow – so we have built it into our admin console. The ‘admin’ console on the cluster details page provides access to the operations list on your MongoDB server:

MongoDB - long running ops


You can also select a particular operation and choose to terminate it. As always exercise this option with discretion:

Terminate long running operations in MongoDB

Dharshan is the founder of (formerly He is an experienced MongoDB developer and administrator. He can be reached for further comment at @dharshanrg