How to list all databases in the Mongo shell?


To list all databases in the Mongo shell, you need to use show command. The syntax is as follows −

show dbs;

Let us implement the above syntax for MongoDB. The query is as follows −

> show dbs;

The following is the output −

admin 0.000GB
config 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
test 0.003GB

If you create a new database in MongoDB then it won’t be present in list of databases.

Let us check the same. Create a new database −

> use studentTracker;
switched to db studentTracker

Now list all the databases from Mongo shell. The query is as follows −

> show dbs;

The following is the output −

admin 0.000GB
config 0.000GB
local 0.000GB
sample 0.000GB
sampleDemo 0.000GB
test 0.003GB

Look at the above sample output, the database ‘studentTracker’ is not present in database list. This is because the database is empty and it isn’t having any collection −

Note: In order to display the newly created database in the list, you need to create a collection in the new database.

Updated on: 30-Jul-2019

648 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements