Skip to content

Instantly share code, notes, and snippets.

@kylemclaren
Created November 20, 2016 07: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 kylemclaren/83c5e4be4d71eed04427b04cc7c58946 to your computer and use it in GitHub Desktop.
Save kylemclaren/83c5e4be4d71eed04427b04cc7c58946 to your computer and use it in GitHub Desktop.
MongoDB Collections sizes pretty printed 🌟
use mydb;
// Container class
function CollStats(name, storageSizeGB, indexSizeGB, totalSizeGB) {
this.name = name;
this.storageSizeGB = storageSizeGB.toFixed(0);
this.indexSizeGB = indexSizeGB.toFixed(0);
this.totalSizeGB = totalSizeGB.toFixed(0);
}
CollStats.prototype.toString = function toStr() {
var s = this.name + ', storage = ' + this.storageSizeGB + ' GB, index = ' +
this.indexSizeGB + ' GB, total = ' + this.totalSizeGB + ' GB';
return s;
}
var bytesInGB = 1024 * 1024 * 1024;
var collectionNames = db.getCollectionNames();
var collStats = [];
for (i = 0; i < collectionNames.length; i++) {
coll = collectionNames[i];
s = db[coll].stats();
var storageSizeGB = s['storageSize'] / bytesInGB;
var indexSizeGB = s['totalIndexSize'] / bytesInGB;
var totalSizeGB = storageSizeGB + indexSizeGB;
var cs = new CollStats(s['ns'], storageSizeGB, indexSizeGB, totalSizeGB);
collStats.push(cs);
}
// descending order sort
collStats.sort(function compare(a, b) {
return b.totalSizeGB - a.totalSizeGB;
});
for (var i = 0; i < collStats.length; i++) {
print(collStats[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment