How to list databases and show collections in MongoDB

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
the show collections command in the  mongodb shell

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.

A list of collections visualized as a diagram made with Moon Modeler - data modeling tool for noSQL databases.

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:

A new MongoDB connection

and then click on Connect and load the existing structure.

Load existing MongoDB database and show collections

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.

Download Moon Modeler

FAQ

How do I start the MongoDB shell?

Open your terminal or command prompt and enter the MongoDB shell by typing the command mongosh.

How do I show a list of databases in MongoDB?

Simply type show dbs in the MongoDB shell.

How do I show collections in MongoDB database?

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.

Why use a data modeling tool for showing collections?

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.

How do I improve the database structure?

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.

Data model for MongoDB

How do themes and display settings enhance diagram readability?

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.

DATA MODELING TOOLS
DRAW Diagrams and
GENERATE Scripts
Download data modeling tools
Blog Categories
Blog by Platforms