How to Show All Databases in MongoDB Quickly
To show all databases in MongoDB, use the
show dbs command in the MongoDB shell. Alternatively, you can run db.adminCommand({ listDatabases: 1 }) to get detailed database info.Syntax
The main ways to list databases in MongoDB are:
show dbs: A shell command that lists all databases with their sizes.db.adminCommand({ listDatabases: 1 }): A command that returns detailed info about all databases in JSON format.
mongodb
show dbs // or db.adminCommand({ listDatabases: 1 })
Example
This example shows how to list all databases using the MongoDB shell.
mongodb
mongosh > show dbs // Output might look like: // admin 0.000GB // local 0.000GB // mydb 0.001GB // Using adminCommand: > db.adminCommand({ listDatabases: 1 }) { "databases" : [ { "name" : "admin", "sizeOnDisk" : 83886080, "empty" : false }, { "name" : "local", "sizeOnDisk" : 83886080, "empty" : false }, { "name" : "mydb", "sizeOnDisk" : 1048576, "empty" : false } ], "totalSize" : 178257920, "ok" : 1 }
Output
admin 0.000GB
local 0.000GB
mydb 0.001GB
{
"databases" : [
{ "name" : "admin", "sizeOnDisk" : 83886080, "empty" : false },
{ "name" : "local", "sizeOnDisk" : 83886080, "empty" : false },
{ "name" : "mydb", "sizeOnDisk" : 1048576, "empty" : false }
],
"totalSize" : 178257920,
"ok" : 1
}
Common Pitfalls
Some common mistakes when trying to list databases:
- Running
show dbsoutside the MongoDB shell will not work because it is a shell-specific command. - Databases with no collections or data may not appear in the list because MongoDB does not create empty databases.
- Using
db.getMongo().getDBNames()is deprecated in newer MongoDB versions; prefershow dbsordb.adminCommand({ listDatabases: 1 }).
mongodb
/* Wrong: Using deprecated method */ > db.getMongo().getDBNames() /* Right: Use this instead */ > show dbs /* Or */ > db.adminCommand({ listDatabases: 1 })
Quick Reference
| Command | Description |
|---|---|
| show dbs | Lists all databases with their sizes in the shell |
| db.adminCommand({ listDatabases: 1 }) | Returns detailed JSON info about all databases |
| db.getMongo().getDBNames() | Deprecated method to list databases, avoid using |
Key Takeaways
Use
show dbs in the MongoDB shell to quickly list all databases.For detailed info, use
db.adminCommand({ listDatabases: 1 }).Empty databases without collections won't appear in the list.
Avoid deprecated methods like
db.getMongo().getDBNames().Run these commands inside the MongoDB shell, not in a regular terminal.