List Databases in MongoDB Shell
show dbs
This command will display a list of all the databases currently present in your MongoDB instance. Each database will be displayed with its size in megabytes.
Here's a breakdown of the command:
dbs
: This specifies that you want to display information about the databases.show
: This keyword indicates that you want to display information.
Example:
If you have three databases named "mydb1", "mydb2", and "mydb3", the output of the show dbs
command would be:
admin 0.00
config 0.00
mydb1 1.00
mydb2 2.00
mydb3 3.00
Listing All Databases in the MongoDB Shell
Basic Command:
show dbs
This command will list all the databases in your MongoDB instance, along with their size in megabytes.
admin 0.00
config 0.00
mydb1 1.00
mydb2 2.00
mydb3 3.00
Using adminCommand
:
For more granular control and to retrieve additional information, you can use the adminCommand
method:
db.adminCommand({ listDatabases: 1 })
This command returns a JSON object containing detailed information about each database, including its name, size, and whether it's empty.
{
"databases": [
{
"name": "admin",
"sizeOnDisk": 495616,
"empty": false
},
// ... other databases
],
"totalSize": 12292096,
"ok": 1
}
Retrieving Only Database Names:
If you only need the database names, you can add the nameOnly
option to the adminCommand
:
db.adminCommand({ listDatabases: 1, nameOnly: true })
[
"admin",
"config",
"mydb1",
"mydb2",
"mydb3"
]
Note:
- For more advanced operations, consider using MongoDB's driver libraries for your preferred programming language.
- Ensure you have the necessary permissions to view all databases.
Alternative Methods for Listing Databases in MongoDB Shell
While the show dbs
and adminCommand
methods are the most common ways to list databases in the MongoDB shell, there are a few alternative approaches you can consider:
Using MongoDB Compass
- Database List: You can easily view all databases in your instance directly from the dashboard.
- Graphical Interface: MongoDB Compass is a visual tool that provides a user-friendly interface for managing MongoDB databases.
Using MongoDB Shell's db Object
- Example:
This will return an array of database names.db.getMongo().getDBs()
- Direct Access: The
db
object represents the current database in the shell. While it doesn't explicitly list all databases, you can use it to switch between databases and examine their properties.
Using MongoDB's Driver Libraries
- Example:
In Python's PyMongo driver:
from pymongo import MongoClient client = MongoClient() databases = client.list_databases()
- Database Listing Methods: These libraries often offer methods to list databases, sometimes with additional features like filtering or sorting.
- Language-Specific: MongoDB provides driver libraries for various programming languages (e.g., Python, Java, Node.js).
Using MongoDB's REST API
- Endpoint: The endpoint for listing databases is typically:
Replacehttp://localhost:27017/admin/listDatabases
localhost:27017
with your MongoDB instance's address. - HTTP Requests: If you prefer a programmatic approach outside of the shell, you can use MongoDB's REST API to list databases.
database mongodb mongodb-query