Understanding Mongo Shell List Databases Simplified 101

By: Published: February 17, 2022

Mongo Shell List Databases

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. 

MongoDB also comes with the Mongo Shell, which is a command-line interface and an interactive tool that was developed using JavaScript. It is through Mongo Shell that you are able to run queries and interact with your MongoDB databases, documents, and collections. For instance, you will need to know the name of a particular database and its other properties.

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.

Table of Contents

Prerequisites

  • MongoDB installed on your local machine.

Introduction to MongoDB

MongoDB Logo
Image Source

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 there is a high availability of data. 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. 

Key Features of MongoDB

Some of the main features of MongoDB are listed below.

  • Ad-hoc Query: MongoDB supports ad-hoc queries that allow users to make queries that are not known while designing or structuring the database.
  • Utility Tools: MongoDB comes with many admin utility nad monitoring tools such as mongodump, mongorestore, mongoimport, etc.
  • Schema Free: MongoDB is a NoSQL Database which means it does not have any restrictions on the schema of the Database.
  • Scalability: With the help of the sharding technique, MongoDB distributes the larger Datasets into smaller chunks of data pieces.

To learn more about MongoDB, click here.

Simplify Data Analysis with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to load data from any data source such as MongoDB, SaaS applications, Cloud Storage, SDK,s, and Streaming Services and simplifies the ETL process. It supports 100+ data sources (including 30+ free data sources) and is a 3-step process by just selecting the data source, providing valid credentials, and choosing the destination. Hevo not only loads the data onto the desired Data Warehouse/destination but also enriches the data and transforms it into an analysis-ready form without having to write a single line of code.

Get Started with Hevo for Free

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensures that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different BI tools as well.

Check out why Hevo is the Best:

  1. Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  2. Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  3. Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  4. Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  5. Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  6. Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, E-Mail, and support calls.
  7. Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

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 databases 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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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
Image Source: Self

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. 

Visit our Website to Explore Hevo

There are different mongo shell list Databases commands in MongoDB. These commands can be run on the Mongo Shell to return the list of databases running on your MongoDB server. The show dbs Mongo Shell command returns the list of all databases running on MongoDB Server including default and user-defined databases. The Mongo Shell db.adminCommand allows you to return the list of MongoDB databases in a JSON format. The command also takes different options to help you filter the databases based on what you need. 

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 100+ 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!

Nicholas Samuel
Freelance Technical Content Writer, Hevo Data

Skilled in freelance writing within the data industry, Nicholas is passionate about unraveling the complexities of data integration and data analysis through informative content for those delving deeper into these subjects. He has written more than 150+ blogs on databases, processes, and tutorials that help data practitioners solve their day-to-day problems.

No-code Data Pipeline For your Data Warehouse