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 Show a List of Databases in PL/SQL?
Managing databases is a fundamental aspect of database administration and development. In Oracle Database, schemas represent logical containers for database objects like tables, views, procedures, and functions. PL/SQL is the procedural extension of and used in Oracle Database and provides powerful
5 min read
How to Show/List Tables in MySQL Database
In MySQL, the SHOW TABLES command is a powerful tool used to list the tables within a specific database. This command provides a convenient way to view the tables that exist in a database without needing to query the database schema directly. In this article, we are going to explore various ways whe
5 min read
How to Secure the MongoDB Database
In todayâs digital era, securing databases is more critical than ever, especially for organizations storing sensitive user and business data. MongoDB, a widely used NoSQL database, requires robust security measures to prevent unauthorized access, data breaches, and cyber threats.By default, MongoDB
10 min read
How to list the Tables in a SQLite Database File ?
SQLite is a database engine which is written in C programming language. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is
4 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
How to Get the Database URL in MongoDB
When working with MongoDB, it is important to know how to retrieve the database URL because as it enables applications to connect to the database. In this article, we'll explore how to obtain the database URL in MongoDB by covering various scenarios and providing examples.How to Get the Database URL
3 min read