Last active
March 13, 2024 07:01
-
-
Save alexbevi/d89d8ce406e7fcea9f0915b7a7580c28 to your computer and use it in GitHub Desktop.
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
/* | |
* Print storage details for all collections and indexes. | |
* Supports sharded clusters | |
* | |
* @author [email protected] | |
* @version 1.3 | |
* @updated 2022-11-21 | |
* | |
* History: | |
* 1.3 - Filter out admin, local and config databases | |
* 1.2 - Properly filter out views | |
* 1.1 - Include Document Count / Average Object Size | |
* 1.0 - Initial Release | |
*/ | |
var fmt = function (bytes) { | |
// comment this out to format the results | |
return bytes; | |
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | |
if (bytes == 0) return '0 Byte'; | |
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); | |
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; | |
} | |
var getDetail = function (label, stats) { | |
var detail = { | |
name: label, | |
count: stats.count, | |
avgSize: stats.avgObjSize, | |
size: stats.size, | |
storageSize: stats.storageSize, | |
reusableSpace: stats.wiredTiger["block-manager"]["file bytes available for reuse"], | |
indexSpace: stats.totalIndexSize, | |
indexReusable: 0, | |
}; | |
var indexKeys = Object.keys(stats.indexDetails); | |
for (var i = 0; i < indexKeys.length; i++) { | |
detail.indexReusable += stats.indexDetails[indexKeys[i]]["block-manager"]["file bytes available for reuse"]; | |
} | |
return detail; | |
} | |
var dbSizeReport = function (dbname) { | |
var results = [] | |
db.getSiblingDB(dbname).getCollectionInfos({ type: "collection" }, { nameOnly: true }).forEach(function(c) { | |
var coll = db.getSiblingDB(dbname).getCollection(c.name); | |
var s = coll.stats({ | |
indexDetails: true | |
}); | |
if (s.hasOwnProperty("sharded") && s.sharded) { | |
var shards = Object.keys(s.shards); | |
for (var i = 0; i < shards.length; i++) { | |
var shard = shards[i]; | |
var shardStat = s.shards[shard]; | |
results.push(getDetail(s.ns + " (" + shard + ")", shardStat)); | |
} | |
} else { | |
results.push(getDetail(s.ns, s)); | |
} | |
}); | |
var totals = [0, 0, 0, 0, 0]; | |
print(["Namespace", "Total Documents", "Average Document Size", "Uncompressed", "Compressed", "Reusable from Collections", "Indexes", "Reusable from Indexes"].join(",")) | |
for (var i = 0; i < results.length; i++) { | |
var row = results[i]; | |
print([row.name, row.count, row.avgSize, fmt(row.size), fmt(row.storageSize), fmt(row.reusableSpace), fmt(row.indexSpace), fmt(row.indexReusable)].join(",")) | |
totals[0] += row.size; | |
totals[1] += row.storageSize; | |
totals[2] += row.reusableSpace; | |
totals[3] += row.indexSpace; | |
totals[4] += row.indexReusable; | |
} | |
print(["Total", "", "", fmt(totals[0]), fmt(totals[1]), fmt(totals[2]), fmt(totals[3]), fmt(totals[4])].join(",")); | |
} | |
var IGNORE = ["admin", "local", "config"]; | |
db.getMongo().getDBNames().forEach(function (dbname) { | |
if (IGNORE.indexOf(dbname) < 0) { | |
print("---------------------") | |
print(dbname); | |
print("---------------------") | |
dbSizeReport(dbname); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script can be used to identify how much reclaimable space your MongoDB collections and indexes (using the WiredTiger storage engine) currently have. This space can be reclaimed using
compact
.Sample output of this script:
If run within a sharded cluster, the Namespace will be followed by the shard name in parenthesis.