Count connections by application and client (connection storm diagnosis)
MongoDBPerformanceCommand
// Total, available and rejected connection counts:
db.serverStatus().connections
// Count connections by application name and client IP:
db.getSiblingDB("admin").aggregate([
{ $currentOp: { allUsers: true, idleConnections: true } },
{ $group: { _id: { app: "$appName", client: { $arrayElemAt: [{ $split: ["$client", ":"] }, 0] } }, conns: { $sum: 1 }, active: { $sum: { $cond: ["$active", 1, 0] } } } },
{ $sort: { conns: -1 } },
{ $limit: 20 }
])What it does
Shows the total and available connection counts on the server, then groups open connections by appName and client IP together with how many are active.
When to use it
When the connection count jumps suddenly, on 'connection refused' or pool timeout errors, to find which application server leaks connections.
Caution
idleConnections: true also returns all idle connections, so on systems with thousands of connections the output is large; summarizing with $group matters for that reason. Rows with a null client are internal threads. Each connection consumes roughly 1 MB of memory; a connection storm also grows memory outside the WiredTiger cache.