Home > Database > MongoDB > Show database in MongoDB

Show database in MongoDB

王林
Release: 2023-08-26 20:45:02
forward
1065 people have browsed it

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
Copy after login

The following is the query to display all databases -

> show dbs;
Copy after login
Copy after login

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
Copy after login

Let's first create a collection containing the documents in the "app" database -

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

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

> db.demo.find();
Copy after login

This will produce the following output-

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

This is the query to display all the databases in MongoDB-

> show dbs;
Copy after login
Copy after login

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
Copy after login

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template