Open In App

How to List all Databases in the Mongo Shell?

Last Updated : 21 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Knowing how to list databases in MongoDB is an important part of managing your data effectively. By using basic MongoDB shell commands, you can easily see what databases you have and understand their sizes.

By using commands such as show dbs and db.stats() and users can gain valuable insights into their MongoDB environment. In this article, We will explore the steps involved in listing all databases in MongoDB and so on.

How to Show the Entire Database in MongoDB?

Managing databases in MongoDB involves knowing how to list all databases. This basic task helps users see the available databases and their sizes by giving a clear picture of the database setup.

By using commands like show dbs and db.stats(), users can get detailed information such as database size, collections and the number of documents.

Steps to List All Databases in the Mongo Shell

Step 1: Create Databases

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

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

use my_database

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

Example:

use NoSQL
use Employee
use registration
use employee

Step 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.

Step 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.

Step 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.

Step 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.

Step 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, understanding how to list databases in MongoDB is vital for effective database management. Utilizing MongoDB shell commands allows users to gain insights into database structures and sizes, facilitating better decision-making. By using these skills, users can enhance their data organization and improve the overall performance of their MongoDB applications.


Next Article

Similar Reads