dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#88
Script #88
mongodb
Monitoringread-onlyMin Version: 4.0

MongoDB Connections by Client and Application

Groups open connections by client address and application name. Used to find the service leaking connections.

#connections#clients#currentOp
mongodb · javascript
db.getSiblingDB("admin").aggregate([
  { $currentOp: { allUsers: true, idleConnections: true } },
  { $group: { _id: { app: "$appName", client: { $arrayElemAt: [{ $split: ["$client", ":"] }, 0] } },
              connections: { $sum: 1 }, active: { $sum: { $cond: ["$active", 1, 0] } } } },
  { $sort: { connections: -1 } },
  { $limit: 20 }
]);
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

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.