Skip to content

Instantly share code, notes, and snippets.

@yelinaung
Last active April 14, 2022 10:11
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 yelinaung/c4a4904f705b4e4a9343d8f61e7f79d4 to your computer and use it in GitHub Desktop.
Save yelinaung/c4a4904f705b4e4a9343d8f61e7f79d4 to your computer and use it in GitHub Desktop.
// YELIN MONGO UTILS
// get index usage
db.orders.aggregate( [ { $indexStats: { } } ] )
// find connection status
ar status = db.serverStatus()
status.connections
// kill operations taking longer than 5s
db.currentOp().inprog.forEach(
function(op) {
if(op.secs_running > 5) db.killOp(op.opid);
}
)
// find operations taking longer than 5s
db.currentOp().inprog.forEach(
function(op) {
if(op.secs_running > 5) printjson(op);
}
)
// CURRENT CONNECTION COUNT
db.currentOp(true).inprog.reduce(
(accumulator, connection) => {
ipaddress = connection.client ? connection.client.split(":")[0] : "Internal";
accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1;
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
)
db.currentOp(true).inprog.filter(connection => !connection.client).map(connection => connection.desc);
// LIST OF INDEXES
db.getCollectionNames().forEach(function(collection) {
indexes = db.getCollection(collection).getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
// INDEX STATS
db.listing.aggregate({ $indexStats: { } }).cursor.firstBatch.forEach(function(item) {
print("Index name : " + item.name)
print("Number of times being accesses : " + item.accesses.ops);
print("-----------------------------")
});
// get collection size
var collectionNames = db.getCollectionNames(),
stats = [];
collectionNames.forEach(function (n) {
stats.push(db[n].stats());
});
// COLLECTION SIZE
for (var c in stats) {
// skip views
if (!stats[c]["ns"]) continue;
print(stats[c]["ns"] + ": " + stats[c]["size"]);
}
// ITEM COUNT IN EACH COLLECTION
db.getCollectionNames().forEach(k => print(db[k].getName(), db[k].count()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment