Home  >  Article  >  Database  >  Show database in MongoDB

Show database in MongoDB

王林
王林forward
2023-08-26 20:45:021036browse

Show database in MongoDB

To display the number of databases in MongoDB, you need to create at least one document in the database.

Suppose you have created a database but have not added any documents to it. Then that particular database will not be visible in the database list.

The following is the query to create the database -

> use app;
switched to db app

The following is the query to display all databases -

> show dbs;

This will produce the following output. The new database "app" will not be visible because we have not added at least 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's first create a collection containing the documents in the "app" database -

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

The following is the query to display all the documents in the collection with the help of find() method-

> db.demo.find();

This will produce the following output-

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

This 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 database list -

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

The above is the detailed content of Show database in MongoDB. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete