Why SHOW DBS does not show my databases in MongoDB?


This SHOW DBS command won’t show the databases because you may have not created a document for a collection. If you will create document for a collection then the created database will be visible.

Let us implement the above concept and create a database −

> use web;
switched to db web

Following is the query to show all databases −

> show dbs;

This will produce the following output −

admin 0.001GB
config 0.000GB
local 0.000GB
my 0.001GB
sample 0.001GB
sampleDemo 0.000GB
studentSearch 0.000GB
test 0.010GB
university 0.000GB

Above, the WEB database is not visible since we haven’t created a collection in the same database.

In order to display WEB database, we need to create a collection with documents in the same database as shown below −

> db.check.insertOne({"ClientName":"John"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cb806c2623186894665ae35")
}

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

> db.check.find();

This will produce the following output −

{ "_id" : ObjectId("5cb806c2623186894665ae35"), "ClientName" : "John" }

Let us execute the above command to display all databases −

> show dbs;

This will produce the following output −

admin 0.001GB
config 0.000GB
local 0.000GB
my 0.001GB
sample 0.001GB
sampleDemo 0.000GB
studentSearch 0.000GB
test 0.010GB
university 0.000GB
web 0.000GB

Look at the above sample output, WEB database is now visible.

raja
Published on 30-Apr-2019 08:47:08
Advertisements