Created
November 20, 2016 07:11
-
-
Save kylemclaren/83c5e4be4d71eed04427b04cc7c58946 to your computer and use it in GitHub Desktop.
MongoDB Collections sizes pretty printed π
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
use mydb; | |
// Container class | |
function CollStats(name, storageSizeGB, indexSizeGB, totalSizeGB) { | |
this.name = name; | |
this.storageSizeGB = storageSizeGB.toFixed(0); | |
this.indexSizeGB = indexSizeGB.toFixed(0); | |
this.totalSizeGB = totalSizeGB.toFixed(0); | |
} | |
CollStats.prototype.toString = function toStr() { | |
var s = this.name + ', storage = ' + this.storageSizeGB + ' GB, index = ' + | |
this.indexSizeGB + ' GB, total = ' + this.totalSizeGB + ' GB'; | |
return s; | |
} | |
var bytesInGB = 1024 * 1024 * 1024; | |
var collectionNames = db.getCollectionNames(); | |
var collStats = []; | |
for (i = 0; i < collectionNames.length; i++) { | |
coll = collectionNames[i]; | |
s = db[coll].stats(); | |
var storageSizeGB = s['storageSize'] / bytesInGB; | |
var indexSizeGB = s['totalIndexSize'] / bytesInGB; | |
var totalSizeGB = storageSizeGB + indexSizeGB; | |
var cs = new CollStats(s['ns'], storageSizeGB, indexSizeGB, totalSizeGB); | |
collStats.push(cs); | |
} | |
// descending order sort | |
collStats.sort(function compare(a, b) { | |
return b.totalSizeGB - a.totalSizeGB; | |
}); | |
for (var i = 0; i < collStats.length; i++) { | |
print(collStats[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment