Skip to content

Instantly share code, notes, and snippets.

@alexzaporozhets
Created February 24, 2015 11:29
Show Gist options
  • Save alexzaporozhets/09d83ef80023c2464bc3 to your computer and use it in GitHub Desktop.
Save alexzaporozhets/09d83ef80023c2464bc3 to your computer and use it in GitHub Desktop.
Mongdb collections ordered by size
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) {
print(stats[c]['ns'] + ": " + bytesToSize(stats[c]['size']) + " (" + bytesToSize(stats[c]['storageSize']) + ")");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment