Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save look4regev/199558ab67682c26bb037471b7fe626e to your computer and use it in GitHub Desktop.
Save look4regev/199558ab67682c26bb037471b7fe626e to your computer and use it in GitHub Desktop.
mongodb group by connections count on env var
// Assuming "application" is the connection env var to group by connections by.
db.currentOp(true).inprog.reduce(
(accumulator, connection) => {
application = connection.clientMetadata && connection.clientMetadata.application ?
connection.clientMetadata.application.name : "Internal";
accumulator[application] = (accumulator[application] || 0) + 1;
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment