Skip to content

Instantly share code, notes, and snippets.

@dungmanh88
Created December 16, 2016 09:05
Show Gist options
  • Save dungmanh88/36bbe47aa7b444fec34ab2176bea7d0a to your computer and use it in GitHub Desktop.
Save dungmanh88/36bbe47aa7b444fec34ab2176bea7d0a to your computer and use it in GitHub Desktop.
Count collection in MongoDB
vi mongodb_collection_size.js
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]['count'] + ":" + stats[c]['size'] + "(" + stats[c]['storageSize'] + ")"); }
mongo --port 27017 -u myUserAdmin -p abc123 --authenticationDatabase admin <DB> mongodb_collection_size.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment