Skip to content

Instantly share code, notes, and snippets.

@follmann
Forked from loopj/mongo-functions.js
Last active October 13, 2015 12:08
Show Gist options
  • Save follmann/4193738 to your computer and use it in GitHub Desktop.
Save follmann/4193738 to your computer and use it in GitHub Desktop.
Useful Mongo Shell Functions
DB.prototype.getCurrentOpStats = function() {
intervals = [1,5,10,30]
waitingForLock = 0;
secsRunningStats = {};
inProg = db.currentOp()["inprog"]
inProg.forEach(function (op) {
if(op["waitingForLock"]) {
waitingForLock += 1;
}
if(op["secs_running"]) {
intervals.forEach(function (interval) {
if(op["secs_running"] > interval) {
secsRunningStats[interval] = secsRunningStats[interval] || 0;
secsRunningStats[interval] += 1;
}
});
}
});
print("total = " + inProg.length);
print("waitingForLock = " + waitingForLock);
for(var i in secsRunningStats) {
print("secs_running > " + i + " = " + secsRunningStats[i]);
};
}
DB.prototype.killLongRunningOps = function(time) {
db.currentOp()["inprog"].forEach(function (op) {
if(op["secs_running"]) {
if(op["secs_running"] > time) {
print("Killing this op (running " + op["secs_running"] + " seconds)");
print(tojson(op))
print("")
db.killOp(op["opid"])
}
}
});
}
DB.prototype.getLongRunningOps = function(time) {
time = time || 30;
print("\noperations running longer than " + time + " seconds:\n");
db.currentOp()["inprog"].forEach(function (op) {
if(op["secs_running"]) {
if(op["secs_running"] > time) {
print("op '" + op["opid"] + "' running: " + op["secs_running"] + " seconds (" + parseInt(op["secs_running"]/60) + " minutes)");
print(tojson(op));
}
}
});
}
DB.prototype.totalIndexInfo = function() {
var collections = db.getCollectionNames();
print("TOTAL INDEX SIZES:");
var results = []
for (col in collections) {
results.push([collections[col], Math.round(db[collections[col]].totalIndexSize() / 1024 / 1024)]);
}
results = results.sort(function(a, b) {
return b[1] - a[1];
});
for (res in results) {
print(results[res][0] + ": " + results[res][1] + " MB");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment