Skip to content

Instantly share code, notes, and snippets.

@swateek
Created September 25, 2017 16:44
Show Gist options
  • Save swateek/d374a0c2abc0c0cfebc9f19ab0118c10 to your computer and use it in GitHub Desktop.
Save swateek/d374a0c2abc0c0cfebc9f19ab0118c10 to your computer and use it in GitHub Desktop.
Use to calculate sizes of MongoDB collections.
use <dbname>;
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'] + ": " + stats[c]['size'] + " bytes (" + stats[c]['storageSize'] + " bytes)"); // in bytes
//print(stats[c]['ns'] + ": " + stats[c]['size']/1000000 + " MB (" + stats[c]['storageSize']/1000000 + " MB)"); // in MBs
//print(stats[c]['ns'] + ": " + stats[c]['size']/1000000000 + " GB (" + stats[c]['storageSize']/1000000000 + " GB)"); // in GBs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment