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
