MongoDB Show Databases

In this article, we will see list of databases available in MongoDB with sizes. We can get the databases information with different methods.

Commands to run from Mongo shell to get all databases:

show dbs show databases db.adminCommand('listDatabases') db.getMongo().getDBNames()

Commands to run from Mongo shell to get all databases:

mongo --quiet --eval "printjson(db.adminCommand('listDatabases'))"

MongoDB Show Databases examples:

1. Using show dbs and show databases

show dbs admin 0.000GB config 0.000GB local 0.000GB test 0.000GB

or

show databases admin 0.000GB config 0.000GB local 0.000GB test 0.000GB

2. Using db.adminCommand(‘listDatabases’)

db.adminCommand('listDatabases') { "databases" : [ { "name" : "admin", "sizeOnDisk" : 122880, "empty" : false }, { "name" : "config", "sizeOnDisk" : 126976, "empty" : false }, { "name" : "local", "sizeOnDisk" : 430080, "empty" : false }, { "name" : "test", "sizeOnDisk" : 40960, "empty" : false } ], "totalSize" : 720896, "ok" : 1 }

3. By running db.getMongo().getDBNames(). This will give only database names.

db.getMongo().getDBNames() [ "admin", "config", "local", "test" ]

4. Run below command on Linux shell.

mongo –quiet –eval “printjson(db.adminCommand(‘listDatabases’))”

root@mongodb2:~# mongo --quiet --eval "printjson(db.adminCommand('listDatabases'))"

{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 122880, "empty" : false }, { "name" : "config", "sizeOnDisk" : 126976, "empty" : false }, { "name" : "local", "sizeOnDisk" : 430080, "empty" : false }, { "name" : "test", "sizeOnDisk" : 40960, "empty" : false } ], "totalSize" : 720896, "ok" : 1 }