Finding and terminating long-running operations in MongoDB

When your MongoDB becomes unresponsive, it’s imperative that you can quickly identify the cause.

Although there can be many reasons for unresponsiveness, we sometimes find that particularly long-running and/or blocking operations (either initiated by a human or an application) are the culprit. Some examples of common operations that can bog down the database are:

  • operations on unindexed fields

  • index builds

  • expensive map-reduce jobs

One way to quickly see if one or more operations are particularly long-running is to use db.currentOp().

Introducing db.currentOp()

The db.currentOp() method reports in-progress operations on your mongod process. In other words, it will return information on all active operations running on your instance. This allows you to quickly identify long-running and/or blocking operations and focus your attention on problematic areas.

Note: On deployments with authentication enabled, db.currentOp() can only be used by an admin database user. If you have a Dedicated plan with us at MongoLab, you can create an admin database user by navigating to your deployment and clicking on the “admin” database under “System Databases”.

Calling db.currentOp()

The raw output that results from calling db.currentOp() from the mongo shell is somewhat hard to read and filter, so we usually wrap it in some code to make the output easier to use.

Our general “go-to” function call for db.currentOp() returns every operation that has been running for longer than 5 seconds:

We recommend using this function because it provides a comprehensive view of all the slow operations on the MongoDB instance and is easily customizable for your use case. You can change the threshold on a case-by-case basis – 5 seconds is a good starting point. You can also filter by other attributes.

If you pass “true” as a parameter to this method – db.currentOp(true) – it will return a more verbose output, including idle connections and system operations.  However, this typically returns operations you are not interested in.

Example db.currentOp() output

Let’s examine the output from a test deployment.

    "locks": {"^myDB": "R"},
    "ns": "",
    "op": "query",
    "opid": 1349152,
    "query": {"test": 1},
    "secs_running": 15,
    "waitingForLock": true
    "locks": {
      "^": "w",
      "^local": "W",
      "^myDB": "W"
    "ns": "",
    "op": "update",
    "opid": 1344808,
    "query": {},
    "secs_running": 53,
    "waitingForLock": false

Note: We stripped out some of the output in this example. Visit MongoDB’s documentation on db.currentOp() to see the full output.


The example on the left shows an unindexed query that has been running for 15 seconds. While the time required for the query to complete is troublesome, potentially more problematic is its impact the system as a whole, and how it might indirectly slow other operations.

The example on the right shows an update that has been running for 53 seconds, which holds the database write lock. During this time, other read and write operations queue behind the lock.


Once you’ve examined the output, you may decide to terminate certain operations to help stabilize your instance. To terminate operations, grab the “opid” value and use db.killOp(). Using the update query from above, you would run the following:

> db.killOp(1344808)

You’re all set!

The next time your MongoDB is feeling sluggish remember that db.currentOp() can help you save time finding troublesome database operations. We hope you get back up and running quickly!


Subscribe to our e-mail newsletter to receive updates.

8 Responses to Finding and terminating long-running operations in MongoDB

  1. Albert Zheng 2014/04/21 at 4:34 am #

    thx for the post!

  2. CDC 2014/08/05 at 1:09 am #

    Thank you!

  3. Kirk Chen 2015/01/29 at 11:49 am #

    Great article

  4. ivanfioravanti 2015/02/21 at 4:11 pm #

    Super article! Thx a lot!

  5. vikram 2015/11/30 at 4:13 am #

    this below command is awesome:
    b.currentOp().inprog.forEach(function(op) {if(op.secs_running > 700) printjson(op); })

    But i want to get only the opId and op: query in the outpt. How can i get that? Can some please help me.

    • Chris Chang 2015/11/30 at 8:26 am #

      This should work for ya:

      db.currentOp().inprog.forEach( function(op) {
      if(op.secs_running > 0) {
      var miniOp = { opid : op.opid, query : op.query };

  6. abinas roy 2016/05/27 at 12:02 am #

    i don get any values from this

    db.currentOp().inprog.forEach( function(op) { if(op.secs_running > 0) { var miniOp = { opid : op.opid, query : op.query }; printjson(miniOp); } });



  1. Easily find & kill MongoDB operations from MongoLab's GUI - 2014/08/13

    […] few months ago, we wrote a blog post on finding and terminating long-running operations in MongoDB. To help make it even easier for MongoLab users* to quickly identify the cause behind database […]

Leave a Reply