Created
June 3, 2021 11:14
-
-
Save qqilihq/d3fe64b5d19527ade9de10d1b4ad13d7 to your computer and use it in GitHub Desktop.
Get MongoDB collection stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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