Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Last active November 21, 2020 05:48
Show Gist options
  • Save jsjohnst/a6820ad3bdf9282507a4 to your computer and use it in GitHub Desktop.
Save jsjohnst/a6820ad3bdf9282507a4 to your computer and use it in GitHub Desktop.
Calculate total disk usage by MongoDB. storageSize is the actual used space, totalSize is the amount of disk space allocated.
db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1 }).databases;
storageSize = 0;
totalSize = 0;
dbs.forEach(function(database) {
db = db.getSiblingDB(database.name);
stats = db.stats();
storageSize += stats.storageSize;
totalSize += stats.fileSize;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment