This informative post enlists several ways to list databases in MongoDB. Follow this to get all the databases and their relevant information.
How to list down databases
MongoDB supports several methods and commands to get the list of databases on the MongoDB server. In this section, we will provide a deep insight to get the list of databases according to several properties like size, names, authorization, and many more.
Getting the list of available databases
The most used Mongo commands to get all the databases are listed below:
Using show dbs and show databases: Execute the following command to check the databases on your mongo server. The output contains default databases as well.
Note: By default, three databases are present on every Mongo shell, they are named as admin, config, local.
Or the below stated command will also show the names of available databases. You will also get default as well as user-defined databases here.
Note: It is to notice that if you create a database by executing the “use” command of MongoDB. You will not find the database by using the above commands unless you add some data/documents into it.
Using getMongo().getDBNames() method: Mongo CLI allows you to execute the getMongo() method and getDBNames() method that shows the list of databases on your MongoDB server:
Getting the list of available databases as a JSON response: MongoDB provides output in JSON response and you can also get the list of available databases by using the following command. You can get the name, sizeOnDisk, and empty status of each database in a JSON response.
At the end of the output, you will observe the totalSize (in bytes) of all the databases is shown. Moreover, you can also pass “1” for ascending order in the output: The below-mentioned command of db.adminCommand will present the same output as the default value of the order is ascending.
The command written above supports various options as well.
Get authorized databases: The “adminCommand” of MongoDB has an option “authorizedDatabases” and this option accepts Boolean values (true/false).
The true value shows the list of authorized databases as shown in the command written below:
As I am using a root account so I have the authorization to use all databases. Moreover, the default value of this option is set to false.
Names of databases: Sometimes, you only need the names of databases instead of getting a detailed output. To do so, we have used the command provided below:
Note: Notice that, with every command, you must have to pass “listDatabases” to apply any option on listDatabases.
Using Regular Expressions with db.adminCommand() expression : You can specify a regular expression on db.adminCommand() to get a more filtered version of the command. For instance, the below stated command will look for those databases that starts with “lin” and will retrieve three documents that are matching the filter:
Or you can use regular expressions to match the exact name or contain all the letters of a word in a database name. In our case the following command matches two databases that exactly contains “linuxhint” word:
MongoDB supports various helpful commands to process the stored data or get the information related to instances of MongoDB. In this article of the MongoDB series, you would have learned to get the list of available databases and can get the information (like, number of collections, size, no.of documents) about them. The information that can be acquired may include names, size, authorized databases, empty databases, and much more. By following this article, you can get to know about all the commands and their relevant options to attain the information related to databases.