Display databases in MongoDB


To display number of databases in MongoDB, you need to create atleast one document in a database.

Let’s say, you have created a database, but did not added any document in it. Then in the list of databases that particular database won’t be visible.

Following is the query to create a database −

> use app;
switched to db app

Following is the query to display all databases −

> show dbs;

This will produce the following output. The new database “app” won’t be visible since we haven’t added atleast one document in it −

admin                0.002GB
business             0.000GB
config               0.000GB
local                0.000GB
main                 0.000GB
my                   0.001GB
sample               0.003GB
sampleDemo         0.000GB
studentSearch 0.000GB
test 0.022GB
university 0.000GB
web 0.001GB
webcustomertracker 0.000GB

Let us first create a collection with documents in the “app” database −

> db.demo.insert({"StudentName":"Chris"});
WriteResult({ "nInserted" : 1 })

Following is the query to display all documents from a collection with the help of find() method −

> db.demo.find();

This will produce the following output −

{ "_id" : ObjectId("5e07250e25ddae1f53b62204"), "StudentName" : "Chris" }

Here is the query to display all the databases in MongoDB −

> show dbs;

This will produce the following output. Now the “app” database will be visible in the list of databases −

admin                      0.002GB
app                        0.000GB
business                   0.000GB
config                     0.000GB
local                      0.000GB
main                       0.000GB
my                         0.001GB
sample                     0.003GB
sampleDemo                 0.000GB
studentSearch            0.000GB
test 0.022GB
university 0.000GB
web 0.001GB
webcustomertracker 0.000GB

Updated on: 31-Mar-2020

561 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements