How to list databases in MongoDB

MongoDB is a NoSQL database to store and manipulate different kinds of data. MongoDB databases store data in the form of documents and support hundreds of methods and operators, to store and retrieve documents. While working on a database, you must be aware of the database name and its general properties. For this, you can easily list down the available databases and MongoDB provides multiple options to check the size of databases, the authorized databases or to look for empty databases.

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.

> show dbs

Diagram Description automatically generated

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.

> show databases

Text Description automatically generated

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:

> db.getMongo().getDBNames()

Text Description automatically generated

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.

> db.adminCommand('listDatabases')

Text Description automatically generated
Text Description automatically generated

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.

> db.adminCommand({listDatabases: 1})

Text Description automatically generated

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:

> db.adminCommand({listDatabases: 1, authorizedDatabases: true})

Text Description automatically generated

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:

> db.adminCommand({listDatabases: 1, nameOnly: true})

Text Description automatically generated

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:

> db.adminCommand({listDatabases: 1, filter: {"name": /^lin/}})

Text Description automatically generated

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:

> db.adminCommand({listDatabases: 1, filter: {"name": /linuxhint/}})

Text Description automatically generated


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.

About the author

Adnan Shabbir