dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#81
Script #81
mongodb
Performanceread-onlyMin Version: 4.0

Find Long-Running Operations in MongoDB (currentOp)

Lists active operations running longer than 5 seconds with plan summary and lock-wait info. Helps pick the operation to kill during a slowdown.

#currentOp#long-running#incident
mongodb · javascript
db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true, idleConnections: false } },
  { $match: { active: true, secs_running: { $gt: 5 }, ns: { $not: /^(admin|local|config)\./ } } },
  { $project: { opid: 1, op: 1, ns: 1, secs_running: 1, client: 1, planSummary: 1,
                waitingForLock: 1, command: 1 } },
  { $sort: { secs_running: -1 } }
]);
Operational Notes & Remediation Guidance

To terminate a long-running operation in MongoDB: `db.killOp(opid)`.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 4.0).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.