Skip to content

Instantly share code, notes, and snippets.

@liberborn
Last active February 9, 2023 17:35
Show Gist options
  • Select an option

  • Save liberborn/f5754bb596c6162a04c5cf902e673ef8 to your computer and use it in GitHub Desktop.

Select an option

Save liberborn/f5754bb596c6162a04c5cf902e673ef8 to your computer and use it in GitHub Desktop.

How to get MongoDB statistics for all collections?

Useful script to find out MongoDB collections size and other statistics.

Studio 3T > IntelliShell

var dbs = db.getMongo().getDBNames();

for (adb of dbs) {
    var colls = db.getSiblingDB(adb).getCollectionNames()

    for (coll of colls) {
        var info = db.getSiblingDB(adb).getCollection(coll).stats(1024);
        if (info.ok) {
            printjson({
                ns: info.ns,
                count: info.count,
                size: info.size,
                storageSize: info.storageSize,
                nindexes: info.nindexes,
                totalIndexSize: info.totalIndexSize
            });
        }
    }
}
  • Double click on storageSize column to sort by size descending

Reference: How to get MongoDB statistics for all collections? #Studio3T_AMA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment