Managing Long-Running Operations in MongoDB®

2 min read
Managing Long-Running Operations in MongoDB®


MongoDB® 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

For more information, please visit Connect with ScaleGrid on LinkedIn, X, Facebook, and YouTube.
Table of Contents

Stay Ahead with ScaleGrid Insights

Dive into the world of database management with our monthly newsletter. Get expert tips, in-depth articles, and the latest news, directly to your inbox.

Related Posts

Redis vs Memcached in 2024

Choosing between Redis and Memcached hinges on specific application requirements. In this comparison of Redis vs Memcached, we strip away...

multi cloud plan - scalegrid

Plan Your Multi Cloud Strategy

Thinking about going multi-cloud? A well-planned multi cloud strategy can seriously upgrade your business’s tech game, making you more agile....

hybrid cloud strategy - scalegrid

Mastering Hybrid Cloud Strategy

Mastering Hybrid Cloud Strategy Are you looking to leverage the best private and public cloud worlds to propel your business...


Add Headline Here