Skip to content

Instantly share code, notes, and snippets.

@vladzloteanu
Created February 29, 2016 19:19
Show Gist options
  • Save vladzloteanu/f91b47c99d3c28853f6a to your computer and use it in GitHub Desktop.
Save vladzloteanu/f91b47c99d3c28853f6a to your computer and use it in GitHub Desktop.
function getReadableFileSizeString(fileSizeInBytes) {
var i = -1;
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
do {
fileSizeInBytes = fileSizeInBytes / 1024;
i++;
} while (fileSizeInBytes > 1024);
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
};
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db.getCollection(n).stats()); });
function sortCollectionsBy(key) {
print("Sorting by " + key);
print("==================")
stats = stats.sort(function(a, b) { return b[key] - a[key]; });
for (var c in stats) { print(stats[c]['ns'] + ": " + getReadableFileSizeString(stats[c][key])); }
}
sortCollectionsBy('size');
sortCollectionsBy('storageSize');
sortCollectionsBy('totalIndexSize');
sortCollectionsBy('count');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment