Common Commands in MongoDB
Check Size of Tables in a Database
To check the size of tables in a database, use the following command to access the desired database:
use database name
- Replace the database name with the actual name of the database you want to access, for example:
use mdworkflow
.
function getReadableFileSizeString(fileSizeInBytes) {
var i = -1;
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
do {
fileSizeInBytes = fileSizeInBytes / 1024;
i++;
} while (fileSizeInBytes > 1024);
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
};
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
stats = stats.sort(function(a, b) { return b['count'] - a['count']; });
for (var c in stats) { print(stats[c]['ns'] + " , " + stats[c]['count'] + " ," + getReadableFileSizeString(stats[c]['storageSize']) + ""); }
- After running the above command, it will display the size of tables in the current database.