Tuesday, April 30, 2019

Get Size in MongoDB

Collections Size of a DB
-------------------------

var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
var stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) { print(stats[c]['ns'] + ": " + stats[c]['size'] + " (" + stats[c]['storageSize'] + ")"); }

DB size
--------

db.adminCommand("listDatabases").databases.sort(function(l, r) {
         return r.sizeOnDisk - l.sizeOnDisk}).forEach(function(d) {
         print(d.name + " - " + d.sizeOnDisk)});

(OR)

db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1 }).databases;
var sum1=0;dbs.forEach(function(database) { sum1+=db.getMongo().getDB(database.name).stats().dataSize }); print(sum1);

Total Size of all DBs
---------------------

var sum = 0; db.getMongo().getDBs()["databases"].forEach(function(x) { sum += db.getMongo().getDB(x.name).stats().dataSize}); print(sum );


No comments:

Post a Comment