Skip to content

Instantly share code, notes, and snippets.

@qqilihq
Created June 3, 2021 11:14
Show Gist options
  • Save qqilihq/d3fe64b5d19527ade9de10d1b4ad13d7 to your computer and use it in GitHub Desktop.
Save qqilihq/d3fe64b5d19527ade9de10d1b4ad13d7 to your computer and use it in GitHub Desktop.
Get MongoDB collection stats
db.getCollectionNames().forEach(function (collectionName) {
const collection = db.getCollection(collectionName);
const stats = collection.stats();
print('COLLECTION: ' + collectionName);
print(' size(MB): ' + (stats.size/1024/1024).toFixed(2));
print(' count: ' + stats.count);
print(' avgObjSize: ' + stats.avgObjSize);
print(' storageSize(MB): ' + (stats.storageSize/1024/1024).toFixed(2));
print(' nindexes: ' + stats.nindexes);
print(' totalIndexSize(MB): ' + (stats.totalIndexSize/1024/1024).toFixed(2));
// per-index stats
const indexStats = collection.aggregate([{$indexStats:{}}]);
indexStats.forEach(function (indexStat) {
print('INDEX: ' + indexStat.name);
print(' size(MB): ' + (stats.indexSizes[indexStat.name]/1024/1024).toFixed(2));
print(' accesses: ' + indexStat.accesses.ops);
print(' since: ' + indexStat.accesses.since.toISOString());
});
print('--------------------------------------------------');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment