Updated March 14, 2023
Introduction to MongoDB List Databases
MongoDB provides the different types of commands to the user, in which that list database is one of the commands that MongoDB provides. Basically, the MongoDB list database command is used to list all existing databases from the current MongoDB server; it can be either system generated or user created. The list database command we run against the admin database. As per the MongoDB version, the list database command may have a different syntax that means it depends on the version of MongoDB. We can perform the list database command on the MongoDB shell as well as the MongoDB compass.
Syntax:
show dbs;
Explanation:
- In the above syntax, we use the show command to list all existing databases on MongoDB as per requirement. Here dbs means database.
How to List Databases in MongoDB?
Given below shows how to list database in MongoDB:
Basically, there are two different ways to list the database one is we can use the show dbs command, and another one is we can use the listDatabase command.
Basically, the listDatabase command is used for the MongoDB application as per our requirement. Normally the listDatabase command is used to list all existing databases with all basic details, but the only difference is that in the listDatabase command, we need to execute the against the admin database by using the following command as follows.
Code:
db.adminCommand( { listDatabases: 1 } )
Explanation:
- In the above statement, we used db.adminCommand with listDatabase command as shown; here, we use 1 for output. When we use 1 with the listDatabase command, then it does not affect the output.
Different options of the listDatabase command are as follows:
- filter: This is an optional part of listDatabase; the filter option is used to determine which database we need to display. The filter has different options such as name, sizeOnDisk, empty, and shared. As per our requirement, we can use any one of the options.
- nameOnly: This is another option in the listDatabase command, and it is optional. A field is used to show whether the order should return simply the data set names or return both data set names and size data. Returning size data requires locking every data set each in turn, while returning just names doesn’t need locking any data set. The default esteem is bogus, so listDatabases returns the name and size data of every information base.
- authorizedDatabases: This is an optional part of the command. This option that figures out which data sets are returned depends on the client’s advantages when access control is empowered. In this option, there are two options as follows. If the user has permission, then it can be able to list all the databases, and if the user does have any permission, then it is able to list all the databases within his specified privileges.
- Comment: The user gave a remark to connect to this order, and this is an optional field of listDatabase command.
Examples of MongoDB List Databases
Given below are the examples of MongoDB List Databases:
But, first, we need to create a different database by using the following command as follows.
Code:
use demo
Explanation:
- In the above statement, we use the “use” command to create the new database. The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now we created another two databases as follows.
Code:
use sample
use temp
Explanation:
- By using the above two statements, we created two new databases named sample and temp.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now let’s see how we can list the database as follows.
For execution, we use the MongoDB command-line tool. Now run the following command to list all databases as follows.
Code:
show dbs;
Explanation:
- In the above command, we use the show dbs command to list the all databases in the MongoDB server; here, dbs is used to list the database.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now let’s see some other commands of the list database as follows.
Code:
db.adminCommand( { listDatabases: 1 } )
Explanation:
- In the above statement, we try to run the listDatabase command against the admin database as shown. Using this statement, we can list all existing database names and sizes as per our requirements.
- One more important thing here we include the 1; here, we use 1 to maintain the stable output of the above statement. The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Without one, we also get the same output, but some show the wrong format; that wise, we recommend using 1.
Code:
db.adminCommand('listDatabases')
Explanation:
- By using the above statement, we try to implement the list database commands the same as the above example. Here the only difference is that we remove the 1 from the statement till we get the same result.
- The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
Now let’s see how we can list the database with only names as follows.
Code:
db.adminCommand( { listDatabases: 1, nameOnly: true} )
Explanation:
- In the above statement, we try to implement the listDatabase command against the admin database as shown, here we use listDatabase command with nameOnly field is true.
- That means when we need to display the only database names at that time, we can use the above statement as per our requirement. The final output of the above statement we illustrated by using the following screenshot as follows.
Output:
So in this way, we can use any filter option with the listDatabase command as per our requirement.
Conclusion
From the above article, we have seen the basic syntax of ListDatabase, and we also saw different examples of ListDatabase. From this article, we have seen how and when we use MongoDB ListDatabase.
Recommended Articles
This is a guide to MongoDB List Databases. Here we discuss the introduction, how to list databases in MongoDB? and examples, respectively. You may also have a look at the following articles to learn more –