How to List all Databases in the Mongo Shell?
Last Updated :
21 Oct, 2024
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:
DatabasesExplanation: 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:
Employee statsExplanation: 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:
CollectionsExplanation: 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.
Similar Reads
How to List all Users in the Mongo Shell In MongoDB, user management is an essential aspect of database administration, allowing administrators to control access and permissions for different users. The Mongo Shell provides a powerful interface to interact with MongoDB including managing users. In this article, we'll explore how to list al
3 min read
How to List All Collections in the MongoDB Shell? Managing collections is a fundamental task in MongoDB database administration. Knowing how to list all collections in the MongoDB shell is essential for understanding your database structure and managing your data effectively. In this article, we'll explore how to list all collections in the MongoDB
3 min read
How to Show a List of All Databases in MySQL MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995.MySQL is repu
7 min read
How to drop all databases present in MongoDb using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to List Databases and Tables in PostgreSQL using PSQL PostgreSQL is a powerful, open-source object-relational database system. It provides a wide array of tools and features to manage databases, tables, and other database objects. In this article, we will explain how to list databases and tables in PostgreSQL using the psql command-line interface. We w
3 min read