MongoDB is a NoSQL database suitable for the storage of different types of data, including unstructured data. In MongoDB, data is stored in the form of documents and collections rather than relations. This gives it the ability to store any type of data.  MongoDB also comes with hundreds of operators and supports different methods for the storage and retrieval of documents. 

To getMongo Shell List Databases, you only have to list the available databases on Mongo Shell. In this article, you will learn about the different common Mongo Shell list database commands and also about listing Databases in JSON format.

Prerequisites

  • MongoDB installed on your local machine.

Introduction to MongoDB

MongoDB Logo

MongoDB is an open-source and the leading NoSQL database. It is a document-oriented database that offers high performance, easy scalability, and high availability. It uses documents and collections to organize data rather than relations. This makes it an ideal database management system for the storage of unstructured data

MongoDB uses replica sets to ensure data availability. Each replica set is made up of two or more replicas of data. This gives its users the ability to access their data at any time. The replica sets also create fault tolerance. MongoDB scales well to accommodate more data. It uses the sharding technique to scale horizontally and meet the changing storage needs of its users. MongoDB was developed to help developers unleash the power of data and software.

Enable seamless MongoDB ETL with Hevo

Hevo is the ideal data pipeline solution for integrating MongoDB as a source, enabling seamless data extraction, transformation, and loading. This ensures smooth data flow and real-time updates, optimizing your analytics and data management processes.

Let’s see some unbeatable features of Hevo Data:

  1. Fully Managed: Hevo Data is a fully managed service and is straightforward to set up.
  2. Schema Management: Hevo Data automatically maps the source schema to perform analysis without worrying about the changing schema.
  3. Real-Time: Hevo Data works on the batch as well as real-time data transfer so that your data is analysis-ready always.  
  4. Live Support: With 24/5 support, Hevo provides customer-centric solutions to the business use case.
Get Started with Hevo for Free

Mongo Shell List Databases 

MongoDB provides a number of ways through which you can get the list of databases stored on your MongoDB server. It’s up to you to choose the most appropriate approach based on your requirements. In this section, we will be discussing the different Mongo Shell list database commands and approaches.

Mongo Shell List Databases: show dbs

You can use the show databases and show dbs commands to get the list of databases in your MongoDB server. The output will also show the list of default databases in the server. Note that every Mongo Shell comes with three default databases namely admin, config, and local. Just run the following command on the Mongo Shell:

show dbs
Mongo Shell List Databases: show dbs

The command shown below will also show available Mongo Shell List Databases. The list will also include default and user-defined databases:

show databases
Mongo Shell List Databases: show databases

Note that if you had created a database using the use command, the above commands will not show the database unless it has some data or documents added to it. 

Mongo Shell List Databases: getMongo().getDBNames()

The Mongo Shell allows you to run the getMongo() and getDBNames() methods to get the list of databases running on your MongoDB server. The following command demonstrates how to use these methods:

db.getMongo().getDBNames()
Mongo Shell List Databases: getMongo().getDBNames() Method output

Listing Databases in JSON Format

MongoDB returns output in a JSON format and you can use the mongo shell list Databases commands to get the Mongo Shell List Databases in a JSON format. For each database, you will get details such as the name of the database, its size on disk, and its empty status in a JSON response. You simply have to use the following command for Mongo Shell List Databases:

db.adminCommand('listDatabases')

The command will also show the total size of the databases in bytes on the disk at the end. 

db.adminCommand JSON format output

If you need to get the output in ascending order, you can pass a parameter of 1 to the command. The following command demonstrates this:

db.adminCommand({listDatabases: 1})

The list of databases will be returned in ascending order as shown below:

db.adminCommand JSON Param 1 format output

Getting Authorized Databases

Mongo Shell’s adminCommand has the option authorizedDatabases which accepts boolean values, that is, true or false. The option returns the list of authorized databases. It can be used as shown in the following command:

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

By default, the option takes a value of false

In some cases, you may only need to know the names of databases running on your MongoDB server. In such a case, you can use Mongo Shell’s adminCommand with the nameOnly parameter. The parameter also takes boolean values, that is, true or false. The following command demonstrates how to use this parameter in a mongo shell list databases command:

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

The command will return the names of databases running on your MongoDB server in ascending order:

authorizedDatabases nameOnly output

Note that we also used the listDatabases option in the command. 

To use the db.adminCommand() in a more filtered version, you can combine it with regular expressions. For example, to get the list of databases whose names begin with “company”, you can combine the db.adminCommand() with the following filter:

db.adminCommand({listDatabases: 1, filter: {"name": /^Company/}})
authorised Databases output

You can also use regular expressions to list databases that contain a particular string in part or in full in their names. For example, the following command will return the list of databases whose names contain the string “ABC”:

db.adminCommand({listDatabases: 1, filter: {"name": /ABC/}})
db.adminCommand authorized Database output

Conclusion

In this article, you learnt about Mongo Shell List Databases, what are the different commands to Mongo Shell List Databases, and list Databases in JSON format. MongoDB is a NoSQL database suitable for the storage of different types of data including unstructured data. It organizes data into documents and collections rather than relations. MongoDB comes with the Mongo Shell, which is a command-line interface that allows you to run queries against your MongoDB databases. 

MongoDB Database stores valuable business data that can be used to generate insights. Companies need to analyze their business data stored in multiple data sources. The data needs to be loaded to the Data Warehouse to get a holistic view of the data. Hevo Data is a No-code Data Pipeline solution that helps to transfer data from 150+ sources to desired Data Warehouse. It fully automates the process of transforming and transferring data to a destination without writing a single line of code.

Want to take Hevo for a spin? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite first hand.

Share your experience of learning about using the Mongo Shell List Databases in the comments section below!

FAQs

1. How do I list only authorized databases?

To see databases you have access to, use the show dbs command after logging in with appropriate credentials.

2. What is the difference between show dbs and listDatabases?

show dbs provides a simple list of databases, while listDatabases offers a detailed output including storage and usage statistics.

3. Do I need admin privileges to list all databases?

Yes, you typically need admin-level privileges to list all databases on the server.

Nicholas Samuel
Technical Content Writer, Hevo Data

Nicholas Samuel is a technical writing specialist with a passion for data, having more than 14+ years of experience in the field. With his skills in data analysis, data visualization, and business intelligence, he has delivered over 200 blogs. In his early years as a systems software developer at Airtel Kenya, he developed applications, using Java, Android platform, and web applications with PHP. He also performed Oracle database backups, recovery operations, and performance tuning. Nicholas was also involved in projects that demanded in-depth knowledge of Unix system administration, specifically with HP-UX servers. Through his writing, he intends to share the hands-on experience he gained to make the lives of data practitioners better.