Home MongoDB How to list databases in MongoDB

How to list databases in MongoDB

by Abraham
list database mongodb

MongoDB is a NoSQL database that can store many forms of data, including unstructured data. Data in MongoDB is stored as collections and documents rather than relations. This enables it to keep any form of data. MongoDB has hundreds of operators and accepts several document storage and retrieval techniques.

MongoDB also includes the Mongo Shell, a command-line interface and interactive tool written in JavaScript. Using Mongo Shell, you may perform queries and engage with your MongoDB databases, collections, and documents. You need to know the database name and its fundamental attributes while working on a database. You may quickly list the available databases for this purpose, and MongoDB has several choices for checking the size of databases, approved databases, or empty databases.

This post will teach you about the various Mongo Shell list database commands and how to list databases in JSON format.


How to list databases in MongoDB

MongoDB provides several methods and commands for retrieving a list of databases on the MongoDB server. In this section, we will go through how to acquire a list of databases based on various criteria like size, name, authorization, and so on.

This section will highlight the most common and widely used Mongo commands that shall be used to list all the available databases in our system:

Using the show dbs and show databases command:

This command is executed to check for any database available on your Mongo server. The output will display all the default databases, three in any Mongo server. The databases are named config, admin, and local.
Execute this line of code to show all the available databases, including the default database:

show dbs
show dbs

Show dbs

Alternatively, you can use the show databases command. To see it in action, execute the following line of code:

show databases
show database

Show database

Using the getMongo().getDBNames() method:

The MongoCLI always allows us to execute the getMongo() method preceded by the getDBNames() method, which will display the list of databases on your MongoDB server. To see how it works, execute this line of code:

using getmongo command to list databases

Using the getMongo command to list databases

Acquisition of Authorized Databases

The authorizedDatabases option of the adminCommand in Mongo Shell allows boolean values, or true or false. The choice provides a list of approved databases. It may be used as displayed in the command below:

db.adminCommand({listDatabases: 1, authorizedDatabases: true})
list authorized databases

List authorized databases

The option is set to false by default.

How to use the db.adminCommand() to retrieve database names

You need to be aware of databases running on your MongoDB server in some circumstances. Once you are aware, you can use Mongo Shell’s adminCommand with the nameOnly argument to retrieve the names of databases. It is essential to ascertain that this option accepts boolean values, which are either true or false. The command below shows how to utilize this argument in the mongo shell list databases command:

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

The command will output the names of databases active on your MongoDB server in ascending order:

retrieve database by name

Retrieve database by name

Note: The listDatabases option was also utilized in the command.

How to use the db.adminCommand() to filter database name results

You may use db.adminCommand() with regular expressions to create a more filtered version. For example, to receive a list of databases whose names begin with “business,” use db.adminCommand() in conjunction with the following filter:

db.adminCommand({listDatabases: 1, filter: {"name": /^fosslinux/}})
filter database by name

Filter database by name

Using Regular Expressions plus db.adminCommand()

You may also use regular expressions to find databases with names that include a certain string in part or in full. The following command, for example, will provide a list of databases whose names contain the text “ABC”:

db.adminCommand({listDatabases: 1, filter: {"name": /fos/}})
using regular expression

Using regular expression

How to retrieve the list of available databases using a JSON response

MongoDB generates output in JSON format, and you may acquire the Mongo Shell List Databases in JSON format by using the mongo shell list Databases commands. In a JSON response, you will receive information about each database, such as its name, size on disk, and empty state. For Mongo Shell List Databases, simply use the following command:


In the end, the program will display the total size of the databases on disk in bytes:

list databases ugin json

List databases ugin JSON

You can supply a parameter of 1 to the command to obtain the results in ascending order. The following command demonstrates it:

db.adminCommand({listDatabases: 1})

As seen below, the list of databases will be displayed in ascending order:

obtain results in ascending order

Obtain results in ascending order


This post taught you about Mongo Shell List Databases, the various commands for Mongo Shell List Databases, and how to list Databases in JSON format. If this article guide was helpful, feel free to share your Mongo Shell List Databases learning experience in the comments below!

You may also like

Leave a Comment



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX



“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.