Open In App

How to List all Databases in the Mongo Shell?

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB is a popular NoSQL database that allows for flexible and scalable data storage. When working with MongoDB, it’s essential to understand how to list all databases to manage them effectively. By using commands such as show dbs and db.stats(), users can gain insights into their database environment, including database sizes and collection counts.

In this article, We will explores the steps involved in listing all databases in MongoDB and interpreting the output and so on.

How to show the Entire Database in MongoDB?

Managing databases in MongoDB requires an understanding of how to list all databases. This fundamental task allows users to view existing databases and their sizes, providing insight into the overall database environment.

By using commands such as show dbs and db.stats(), users can access detailed information about databases, including size, collections, and document counts. Understanding how to list and interpret database information is essential for effective database management in MongoDB.

We will list All Databases in the Mongo Shell by below defined steps:

  1. Create Database
  2. List Database
  3. Interpret Output
  4. Detailed Database Information
  5. Exploring Database Contents
  6. Exit the mongo shell

1. Create Database

First, let’s create some databases using Mongo shell.

Syntax:

To create a new database, you can use the use <database_name> command. Replace <database_name> with the name you want for your database.

use my_database

Create as many databases as you want to create. I had created lots of databases:

Example:

use NoSQL
use Employee
use registration
use employee

2. Listing Databases

The most basic command for listing the databases in the MongoDB shell is ‘show dbs‘. This command prints the list of databases and their sizes. On the other hand, we need to understand that MongoDB has only databases that have data. Empty databases will never show in the output.

show dbs

Output:

AllDatabases

Databases

Explanation: As we have seen in the image that we have List of all the databases which we had created.

3. Interpreting the Output

After executing `show dbs`, MongoDB prints out a list of the databases in our system with the sizes of each database in megabytes (MB). Big data is usually associated with larger databases that facilitate increased data storage and usage. However, size isn’t the only reflection of the complexities and the performance of our MongoDB environment.

4. Detailed Database Information

While show dbs gives a brief but useful outline, you can drill down into specific databases by using db.stats() function. This command displays the categories such as database size, the number of collections, and document count.

use my_database
db.stats()

Example:

use employee
d.stats()

Output:

EmployeeStats

Employee stats

Explanation: As we have seen in the image that it show all the details of the employee database.

5. Exploring Database Contents

Besides listing databases, it is also crucial to interpret the data inside them. Change the database environment with the use command and display all collections within the database using the show collections command. These commands permit easy navigation and control of the MongoDB databases and collections.

use my_database
show collections

Example:

use employee
show collections

Output:

Collections

Collections

Explanation: As we have seen in the image that there are the two collections in employee database.

6. Exiting the MongoDB Shell

Once we have completed our tasks in the MongoDB shell, we can exit. Click on the close (X) button located at the top-right corner of the MongoDB Compass window.

Conclusion

Overall, Listing all databases in MongoDB is a crucial aspect of database management, providing users with valuable information about their database environment. By understanding and using commands such as show dbs and db.stats(), users can effectively manage their databases, ensuring optimal performance and scalability.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads