Last active
January 26, 2021 19:21
-
-
Save dr-dimitru/c0550aa045593a696b4b6f3a8b598408 to your computer and use it in GitHub Desktop.
Show each collection size in MongoDB
This file contains hidden or 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
var getReadableFileSizeString = function(fileSizeInBytes) { | |
var i = -1; | |
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB']; | |
do { | |
fileSizeInBytes = fileSizeInBytes / 1024; | |
i++; | |
} while (fileSizeInBytes > 1024); | |
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i]; | |
}; | |
printCollectionSizes = function () { | |
var collectionNames = db.getCollectionNames(); | |
var stats = []; | |
collectionNames.forEach(function (n) { | |
stats.push(db.getCollection(n).stats()); | |
}); | |
stats = stats.sort(function(a, b) { return b.size - a.size; }); | |
for (var c in stats) { | |
print(stats[c]['ns'] + ": " + getReadableFileSizeString(stats[c]['size']) + " (" + getReadableFileSizeString(stats[c]['storageSize']) + ")" + " (Indexes: " + getReadableFileSizeString(stats[c]['totalIndexSize']) + ")"); | |
} | |
} | |
// Copy-paste code above as it is to MongoDB CLI Shell | |
// Hit [return]/[enter] key to make sure last line passed through | |
// type-in `printCollectionSizes()` | |
// | |
// OUTPUT: | |
// Collection_Name Records_Size (Actual_Storage_Size_On_FS) (Indexes: Indexes_Size_In_Memory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment