liveBook · Manning

Db.currentOp()

As explained in MongoDB 8.0 in Action, Third Edition

db.currentOp() is a MongoDB database method that returns information about currently executing operations.

db.currentOp() is a MongoDB database method for inspecting operations currently executing on a server. Its snapshot can reveal operation type, namespace, client, duration, and resource-related state, helping locate long-running work, contention, or blocked tasks. section 21.2.3

Use filtered output to narrow operational investigations:

  • After identifying an operation ID, stop it with db.killOp(12345) when it consumes excessive resources, causes bottlenecks, or blocks other work. section 21.2.3

Reading an active operation

An output document can show active: true, opid: 12345, secs_running: 45, namespace myDatabase.myCollection, a find command, client address, and waitingForLock: false. The duration and lock state help determine whether an operation warrants investigation.

Listing 7.11 Status of an Index build operation
db.adminCommand(
    {
      currentOp: true,
      $or: [
        { op: "command", "command.createIndexes": { $exists: true }  },
        { op: "command", "command.$truncated": /^\{ createIndexes/  },
        { op: "none", "msg" : /^Index Build/ }
      ]
    }
)

For index-build monitoring, the current-operation query uses db.adminCommand with currentOp: true and conditions matching createIndexes or index-build messages. section 7.5.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains the snapshot, representative fields, duration and operation filters, and escalation through killOp.
Examples
Uses mongosh filters for long-running and insert operations, an active-operation document, and an index-build monitoring command.
What it assumes
Uses mongosh commands and operation IDs without teaching MongoDB command-line basics.
What you can do after
Diagnose/debug with mongosh by filtering active operations, interpreting duration and lock state, and terminating selected work.
How it connects
Connects active-operation monitoring with killOp, serverStatus, lock queues, and index-build management.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.