Script #82
mongodb
Storageread-onlyMin Version: 4.0MongoDB Collection and Index Sizes
Ranks collections by document count, data, storage and index size. Used for capacity planning and spotting bloated indexes.
#collstats#capacity#index-size
mongodb · javascript
const mb = n => +(n / 1024 / 1024).toFixed(1);
const rows = db.getCollectionInfos({ type: "collection" }).map(c => {
const s = db.getCollection(c.name).stats();
return { collection: c.name, docs: s.count, data_mb: mb(s.size),
storage_mb: mb(s.storageSize), index_mb: mb(s.totalIndexSize),
avg_doc_bytes: s.avgObjSize };
}).sort((a, b) => (b.storage_mb + b.index_mb) - (a.storage_mb + a.index_mb));
console.table(rows);Operational Notes & Remediation Guidance
Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.
Production Safety & Execution Standards
- This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
- Prior to executing on production systems under high load, verify the target database version (Min. Version: 4.0).
- Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.
Other MONGODB Diagnostic Scripts
View All (100)#81Performance
Find Long-Running Operations in MongoDB (currentOp)
Lists active operations running longer than 5 seconds with plan summary and lock-wait info. Helps pick the operation to kill during a slowdown.
View Script
#83Performance
Find Unused Indexes in MongoDB ($indexStats)
Lists never-used indexes per collection. Offers cleanup candidates to cut write cost and memory use.
View Script
#84Replication
MongoDB Replica Set Lag and Oplog Window
Shows member state, lag behind the primary and the oplog window. Monitors replication health and resync risk.
View Script
#85Performance
Analyze Slow MongoDB Queries with the Profiler
Groups system.profile data to reveal the slowest query patterns and COLLSCANs. Ideal for finding missing indexes.
View Script