MongoDB query to get the stats of all collections in all databases

To collect the stats of all the collections in all databases from a mongo shell, you can run below query.

 db.getMongo().getDBNames().forEach(function (d) { print(“Database: ” + d ); var curr_db = db.getSiblingDB(d); printjson( curr_db.stats() ); curr_db.getCollectionNames().forEach(function(coll) { print(“.. collection: ” + coll ); var c = curr_db.getCollection(coll); if ( typeof c != “function”) { printjson( c.stats() ); printjson( c.getIndexes() ); } }); });

[root@dbversity.com ~]# /opt/mongodb/bin/mongo –port 27030

MongoDB shell version: 2.4.11

connecting to: 127.0.0.1:27030/test

rs3:PRIMARY>

rs3:PRIMARY>

rs3:PRIMARY>

rs3:PRIMARY> db.getMongo().getDBNames().forEach(function (d) { print(“Database: ” + d ); var curr_db = db.getSiblingDB(d); printjson( curr_db.stats() ); curr_db.getCollectionNames().forEach(function(coll) { print(“.. collection: ” + coll ); var c = curr_db.getCollection(coll); if ( typeof c != “function”) { printjson( c.stats() ); printjson( c.getIndexes() ); } }); });

Database: shrdb

{

        “db” : “shrdb”,

        “collections” : 3,

        “objects” : 6,

        “avgObjSize” : 48.666666666666664,

        “dataSize” : 292,

        “storageSize” : 243322880,

        “numExtents” : 15,

        “indexes” : 2,

        “indexSize” : 16352,

        “fileSize” : 1056702464,

        “nsSizeMB” : 16,

        “dataFileVersion” : {

                “major” : 4,

                “minor” : 5

        },

        “ok” : 1

}

.. collection: mycol

{

        “ns” : “shrdb.mycol”,

        “count” : 0,

        “size” : 0,

        “storageSize” : 243314688,

        “numExtents” : 13,

        “nindexes” : 2,

        “lastExtentSize” : 68579328,

        “paddingFactor” : 1,

        “systemFlags” : 1,

        “userFlags” : 0,

        “totalIndexSize” : 16352,

        “indexSizes” : {

                “_id_” : 8176,

                “user_id_1” : 8176

        },

        “ok” : 1

}

[

        {

                “v” : 1,

                “name” : “_id_”,

                “key” : {

                        “_id” : 1

                },

                “ns” : “shrdb.mycol”

        },

        {

                “v” : 1,

                “name” : “user_id_1”,

                “key” : {

                        “user_id” : 1

                },

                “ns” : “shrdb.mycol”

        }

]

.. collection: system.indexes

{

        “ns” : “shrdb.system.indexes”,

        “count” : 2,

        “size” : 152,

        “avgObjSize” : 76,

        “storageSize” : 4096,

        “numExtents” : 1,

        “nindexes” : 0,

        “lastExtentSize” : 4096,

        “paddingFactor” : 1,

        “systemFlags” : 0,

        “userFlags” : 0,

        “totalIndexSize” : 0,

        “indexSizes” : {

 

        },

        “ok” : 1

}

[ ]

Database: local

{

        “db” : “local”,

        “collections” : 6,

        “objects” : 2976766,

        “avgObjSize” : 147.54530923828074,

        “dataSize” : 439207860,

        “storageSize” : 5954486096,

        “numExtents” : 18,

        “indexes” : 1,

        “indexSize” : 8176,

        “fileSize” : 6456082432,

        “nsSizeMB” : 16,

        “dataFileVersion” : {

                “major” : 4,

                “minor” : 5

        },

        “ok” : 1

}

.. collection: oplog.rs

{

        “ns” : “local.oplog.rs”,

        “count” : 2976756,

        “size” : 439206156,

        “avgObjSize” : 147.54523246110867,

        “storageSize” : 5943971664,

        “numExtents” : 13,

        “nindexes” : 0,

        “lastExtentSize” : 4096,

        “paddingFactor” : 1,

        “systemFlags” : 0,

        “userFlags” : 0,

        “totalIndexSize” : 0,

        “indexSizes” : {

 

        },

        “capped” : true,

        “max” : NumberLong(“9223372036854775807”),

        “ok” : 1

}

[ ]

.. collection: slaves

{

        “ns” : “local.slaves”,

        “count” : 1,

        “size” : 128,

        “avgObjSize” : 128,

        “storageSize” : 8192,

        “numExtents” : 1,

        “nindexes” : 1,

        “lastExtentSize” : 8192,

        “paddingFactor” : 1,

        “systemFlags” : 1,

        “userFlags” : 0,

        “totalIndexSize” : 8176,

        “indexSizes” : {

                “_id_” : 8176

        },

        “ok” : 1

}

[

        {

                “v” : 1,

                “name” : “_id_”,

                “key” : {

                        “_id” : 1

                },

                “ns” : “local.slaves”

        }

]

.. collection: startup_log

{

        “ns” : “local.startup_log”,

        “count” : 1,

        “size” : 976,

        “avgObjSize” : 976,

        “storageSize” : 10485760,

        “numExtents” : 1,

        “nindexes” : 0,

        “lastExtentSize” : 10485760,

        “paddingFactor” : 1,

        “systemFlags” : 0,

        “userFlags” : 0,

        “totalIndexSize” : 0,

        “indexSizes” : {

 

        },

        “capped” : true,

        “max” : NumberLong(“9223372036854775807”),

        “ok” : 1

}

[ ]

.. collection: system.indexes

{

        “ns” : “local.system.indexes”,

        “count” : 1,

        “size” : 72,

        “avgObjSize” : 72,

        “storageSize” : 4096,

        “numExtents” : 1,

        “nindexes” : 0,

        “lastExtentSize” : 4096,

        “paddingFactor” : 1,

        “systemFlags” : 0,

        “userFlags” : 0,

        “totalIndexSize” : 0,

        “indexSizes” : {

 

        },

        “ok” : 1

}

[ ]

.. collection: system.replset

{

        “ns” : “local.system.replset”,

        “count” : 1,

        “size” : 208,

        “avgObjSize” : 208,

        “storageSize” : 8192,

        “numExtents” : 1,

        “nindexes” : 0,

        “lastExtentSize” : 8192,

        “paddingFactor” : 1,

        “systemFlags” : 0,

        “userFlags” : 0,

        “totalIndexSize” : 0,

        “indexSizes” : {

 

        },

        “ok” : 1

}

[ ]

Database: admin

{

        “db” : “admin”,

        “collections” : 0,

        “objects” : 0,

        “avgObjSize” : 0,

        “dataSize” : 0,

        “storageSize” : 0,

        “numExtents” : 0,

       “indexes” : 0,

        “indexSize” : 0,

        “fileSize” : 0,

        “nsSizeMB” : 0,

        “dataFileVersion” : {

 

        },

        “ok” : 1

}

Database: test

{

        “db” : “test”,

        “collections” : 0,

        “objects” : 0,

        “avgObjSize” : 0,

        “dataSize” : 0,

        “storageSize” : 0,

        “numExtents” : 0,

        “indexes” : 0,

        “indexSize” : 0,

        “fileSize” : 0,

        “nsSizeMB” : 0,

        “dataFileVersion” : {

 

        },

        “ok” : 1

}

rs3:PRIMARY>

rs3:PRIMARY>

rs3:PRIMARY>

bye

  • Ask Question