How to list databases and show collections in MongoDB

For every MongoDB user, it’s essential to understand the structure of the database. 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 exists 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 diagram of the database structure.

MongoDB collections in MongoDB diagram - use a diagram instead of the show collections command.

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:

New MongoDB connection

and then click on Connect and load 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

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