Last Updated: February 25, 2016
·
6.654K
· fsamir

MongoDB: determine total size of indexes in a database.

One of the most important things to keep in mind when setting up MongoDB for production environments, is that the total size of indexes fits in the RAM.

This query display the total size of the indexes in a mongo database.

var collections = db.getCollectionNames()

var totalIndexSize = 0;
collections.forEach(function (collectionName) {
    var collection = db.getCollection(collectionName)
    totalIndexSize += collection.totalIndexSize();
});

print("Total size of indexes: " + totalIndexSize + " bytes (" + (totalIndexSize / 1024 ) / 1024 + "MB)");