Understanding the structure of the database is essential for every MongoDB user. In this article, we will explore different ways to achieve this efficiently.
Using the shell to show collections in MongoDB
Connect to MongoDB
To begin, make sure you have MongoDB installed on your system. Open your terminal or command prompt and enter the MongoDB shell by typing the command mongosh
.
mongosh
List MongoDB databases
Once you’re connected to the MongoDB server, you can retrieve the list of databases by using the show dbs
command. Simply type show dbs
in the MongoDB shell, and it will display a list of all the available databases.
show dbs
The show dbs
command will present you with a list of databases, however, it’s important to note that this list may include some databases that are internal to MongoDB and not directly accessible for data storage.
Switching to a database
To switch to a database named “mydatabase”, enter use mydatabase. This will enable you to perform operations within that database.
use restaurant
Retrieving collections using the show collections command
Once you have switched to a specific database, you can retrieve the list of collections within it. Use the show collections
command to show the collections that exist in the currently selected database.
show collections

Using Moon Modeler to show collections in MongoDB and better understand the structure
Another option is to display all collections of the selected database using a data modeling tool that can create a data model and a diagram of the database structure.

The advantage is greater clarity, the ability to search, view details, view validation scripts etc.
Creating a MongoDB diagram in Moon Modeler is easy. Just create a database connection:

and then click on Connect and load the existing structure.

Learn more about visualization of existing MongoDB databases.
Conclusion
Listing databases and showing collections in MongoDB is a fundamental task for any user or developer working with MongoDB. By following the steps outlined in this article, you can quickly retrieve a list of databases, switch to a specific database, and access its collections. Understanding the structure of your MongoDB environment will help you navigate and work effectively with your database structures or data models, especially when you use a data modeling tool like Moon Modeler.
FAQ
Open your terminal or command prompt and enter the MongoDB shell by typing the command mongosh
.
Simply type show dbs
in the MongoDB shell.
In the MongoDB shell, use the show collections
command to show the collections. Another option is to display all collections of the selected database using a data modeling tool that can create a data model and a diagram.
Data modeling tools simplify the design, visualization and maintenance of database structures. You can create not only a diagram that represents a snapshot of the current state of the database structure, but also a data model that can be edited, enhanced, annotated, and used for collaboration purposes.
You can easily extend the structure and add more collections and embedded objects to the data model. Adding elements such as subject areas, notes, lines, images and more will increase the readability and clarity of the data model. You can also split the diagram into sub-diagrams, etc.
Moon Modeler provides a variety of themes and display settings designed to improve diagram clarity and professionalism. Whether you’re crafting internal documentation or presenting to clients, customizable visual styles – including professional layouts and hand-drawn effects – allow you to adapt the diagram’s appearance to your audience, ensuring readability and consistency.