MongoDB Database Sizes and Disk Usage
Lists data, storage and index size for each database. Used for capacity tracking and seeing compression effect.
const gb = n => +(n / 1073741824).toFixed(2);
db.adminCommand({ listDatabases: 1 }).databases.map(d => {
const s = db.getSiblingDB(d.name).stats();
return { db: d.name, data_gb: gb(s.dataSize), storage_gb: gb(s.storageSize),
index_gb: gb(s.indexSize), collections: s.collections, objects: s.objects };
}).sort((a, b) => b.storage_gb - a.storage_gb);Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.
- 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.
Engineering Triad: Related Runbooks & Incident Response
Which production incidents and emergency drills execute this script as a diagnosis step?
MongoDB WiredTiger Cache Pressure: Working Set Does Not Fit in RAM
Reads cache fill, dirty ratio and eviction pressure; finds index and query issues that inflate the working set. Guides decisions on cache size, index cleanup and hardware.
MongoDB Data Disk Full: Safe Space Recovery
Investigates dbPath growth, replica topology, and backup constraints to recover safely from a full MongoDB data disk.
Other MONGODB Diagnostic Scripts
View All (100)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.
MongoDB Collection and Index Sizes
Ranks collections by document count, data, storage and index size. Used for capacity planning and spotting bloated indexes.
Find Unused Indexes in MongoDB ($indexStats)
Lists never-used indexes per collection. Offers cleanup candidates to cut write cost and memory use.
MongoDB Replica Set Lag and Oplog Window
Shows member state, lag behind the primary and the oplog window. Monitors replication health and resync risk.