Listing MongoDB databases (collections)
MongoDB is a highly popular NoSQL database widely used in modern applications. It’s important to note that, unlike relational databases such as MySQL or PostgreSQL, MongoDB doesn’t strictly operate with "databases." Instead, MongoDB organizes data into collections within what it refers to as databases, but its model is designed to be document-oriented and flexible.
Prerequisites
Before you begin, ensure that:
- MongoDB is installed on your system.
- You have access to a MongoDB user with sufficient privileges to view databases (e.g., the default
admin
user or another appropriately configured user).
Command-Line Method
Step 1: Access MongoDB via the Terminal
To access the MongoDB shell (mongosh
), open your terminal or command prompt and run:
mongosh
If your MongoDB instance requires authentication, connect using a specific user and authentication database:
mongosh --username [username] --authenticationDatabase [authDatabase]
- Replace
[username]
with your MongoDB username. - Replace
[authDatabase]
with the database where your user is defined (oftenadmin
). - The shell will prompt you for your password. Enter it and press
Enter
.
If the connection is successful, you’ll see the MongoDB prompt, typically:
test>
Step 2: List Workspaces (Databases)
To view the available workspaces (referred to as databases in MongoDB), use the following command in the MongoDB shell:
show dbs
This command will display a list of databases (or workspaces) along with their approximate sizes. For example:
admin 0.000GB
config 0.000GB
local 0.000GB
my_database 0.002GB
admin
: The database used for administrative tasks, such as user creation and permissions management.config
: Stores metadata for sharded clusters.local
: Contains data specific to the local instance (e.g., replication information).- Custom databases: Any databases (or workspaces) you’ve created or used will also appear here.
Step 3: Exit the MongoDB Shell
When you’re finished in the MongoDB shell, simply type:
exit
Tip: Use a Quick Command
If you prefer a faster method to list databases without entering the interactive shell, run the following command in the terminal:
mongosh --eval "db.adminCommand('listDatabases')"
This will return a JSON-like structure containing the database names and their sizes. For example:
{
"databases": [
{ "name": "admin", "sizeOnDisk": 8192, "empty": false },
{ "name": "config", "sizeOnDisk": 12288, "empty": false },
{ "name": "local", "sizeOnDisk": 40960, "empty": false },
{ "name": "my_database", "sizeOnDisk": 20480, "empty": false }
],
"totalSize": 81920,
"ok": 1
}
Graphical Method: MongoDB Compass
If you prefer a visual method to explore MongoDB databases, you can use MongoDB Compass, the official GUI for MongoDB. Here’s how:
- Download and install MongoDB Compass from the official website: MongoDB Compass.
- Launch MongoDB Compass.
- Connect to your MongoDB instance by entering the connection URI (e.g.,
mongodb://localhost:27017
for a local instance). - Once connected, you’ll see a list of available databases on the left-hand side.
- Click on a database to explore its collections and documents.
MongoDB Compass is ideal for users who prefer to avoid the command line or need a graphical view of their data and structures.
Conclusion
Listing databases (or workspaces) in MongoDB is easy, whether you use the command line with mongosh
or the user-friendly interface of MongoDB Compass. Depending on your preferences, you can choose the method that best suits your workflow. MongoDB remains a flexible solution, designed to meet the needs of both developers and administrators.