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.
Prerequisites
- MongoDB installed on your local machine.
Introduction to MongoDB
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.
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
The command shown below will also show available Mongo Shell List Databases. The list will also include default and user-defined 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()
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.
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:
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})
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:
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/}})
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/}})
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 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.