Skip to content

Instantly share code, notes, and snippets.

@swateek
Created September 26, 2017 08:12
Show Gist options
  • Save swateek/f3fb5ebd7473ff8a8da0fe39ad5fa8b8 to your computer and use it in GitHub Desktop.
Save swateek/f3fb5ebd7473ff8a8da0fe39ad5fa8b8 to your computer and use it in GitHub Desktop.
collection_size_to_csv.js
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']; });
print("collection,size,sizeOnDisk");
for(var c in stats){
print(stats[c]['ns'] + "," + stats[c]['size'] + "," + stats[c]['storageSize']); // in bytes
//print(stats[c]['ns'] + "," + stats[c]['size']/1000000 + "," + stats[c]['storageSize']/1000000); // in MBs
//print(stats[c]['ns'] + "," + stats[c]['size']/1000000000 + "," + stats[c]['storageSize']/1000000000); // in GBs
}
// How to use:
// cat collection_size_to_csv.js | mongo > output.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment