• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:10 mins read
You are currently viewing List Databases in MongoDB

How to list databases in MongoDB? The first step in managing and using the data is to list the databases in MongoDB. In this article, we will walk through the process of listing databases in MongoDB using different methods. List databases give us helpful information about the MongoDB environment and enable us to access particular databases for additional activities.


Note that the legacy mongo shell command mongo was deprecated in MongoDB 5.0 and removed in MongoDB 6.0. The new mongo shell mongosh has improved and has many advantages, for example, compatibility with the MongoDB Node.js driver.

1. List Database Using the ‘show dbs’ command

The most frequently used command is the show dbs command to display a list of databases present in the MongoDB server. When we run the show dbs command in the MongoDB shell or a MongoDB client, it will output a list of database names along with their sizes.

# Display a list of databases
show dbs

Here, we execute the show dbs command which outputs the different databases along their sizes. Please note that the show dbs command only displays the databases that have been created and have data in them. If a database is empty, then it may not be listed.

List database in MongoDB

2. List Database Using the ‘show databases’ command

Alternatively, the ‘show databases’ command can also list the databases in MongoDB. Similar to the show dbs command, the show databases command is used to list the databases that are currently accessible on the MongoDB server.

# Usage of show databases command
show databases

Here, the command listed all the databases in the MongoDB shell which is stored in the MongoDB server. 

3. List Database Using the ‘getDBNames()’ command

The db.getMongo().getDBNames() also be utilized in the MongoDB shell to retrieve an array of database names present in the MongoDB server. It provides a programmatic way to obtain the list of databases using JavaScript within the shell.

# Usage of db.getMongo().getDBNames()

Here, it returns the list of databases below in array format. 

List database in MongoDB

4. List Database Using the ‘administrative’ command

The db.adminCommand('listDatabases') command in the MongoDB shell is an administrative command that is also used to retrieve a document containing information about all the databases available on the MongoDB server. This command is similar to the previous approach but provides more detailed information about each database.

# Usage of db.adminCommand('listDatabases') command

Here, the output returns the document, which contains information about each database, including the name, size, and other details.

Additionally, the listDatabases field with a value of 1 can also be specified to retrieve all the databases in ascending order.

# Adding listDatabases field with value of 1
db.adminCommand({listDatabases: 1})

Here, we run the admin command by passing an object argument listDatabases with value 1 which outputs the list of the databases in ascending order.

List database in MongoDB

However, only the names of the database can also be listed with the administrative command of MongoDB. We just have to pass the argument nameOnly along with the listDatabases argument.

# Passing nameOnly argument along with listDatabases
db.adminCommand({listDatabases: 1, nameOnly: true})

Here, the databases field contains an array of objects below, and each object represents a database with only the name field included.

5. List Database Using the ‘regular expression’ command

Further, the more filtered version of the command can be obtained by using the regular expression parameter on the MongoDB administrative command.

# Usage of regular expressions
db.adminCommand({listDatabases: 1, filter: {"name": /student/}})

Here, we filter the database list using the regular expression /student/ to match the term student in database names. The output listed in the database contains the specified regular expression.

6. List Database Using the ‘stats()’ command

The db.stats() method in MongoDB is another approach that is used to retrieve statistics about the current database. When executed in the MongoDB shell, it returns an object containing various statistics related to the database.

# Usage of db.stats() method

Here, we defined the above command for the default database test which displayed the statistical information which includes the size of the database, the number of collections, the number of documents, and other relevant information.

List database in MongoDB

More details about this method can be found here.

7. Conclusion

In conclusion, we have explained various methods that list the databases stored in the MongoDB server. By utilizing these methods, we can effectively navigate, query, and manipulate the databases within your MongoDB environment.

Keep in mind that databases in MongoDB are created on-demand when you first write data to them. So, if you don’t see a specific database in the list, it may not have been created yet or it may not contain any data.