Complete Guide to Listing All Databases in MongoDB Shell

Nov 14, 2025 · Programming · 9 views · 7.8

Keywords: MongoDB | Database Listing | Shell Commands | listDatabases | show dbs

Abstract: This article provides a comprehensive overview of various methods to list all databases in MongoDB Shell, including basic show dbs command and advanced listDatabases database command. Through comparative analysis of different method scenarios, it deeply explores advanced features like permission control and output format customization, with complete code examples and practical guidance.

Overview of MongoDB Database Listing Methods

In MongoDB database management, viewing all databases present in the current instance is a fundamental yet crucial operation. Unlike relational databases, MongoDB provides specialized built-in commands to accomplish this task.

Basic Command: show dbs

In the MongoDB Shell environment, the most straightforward way to list all databases is using the show dbs command. This command returns a list of all non-empty databases in the current MongoDB instance, along with the disk space occupied by each database.

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

It's important to note that the show databases command is functionally identical to show dbs, and both can be used interchangeably. This design considers different user preferences and provides better user experience.

Alternative Command: show databases

Besides show dbs, MongoDB also provides the more semantically explicit show databases command. These two commands are functionally equivalent and produce identical output results.

> show databases
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

This command alias design reflects MongoDB's emphasis on user-friendliness, allowing users to choose command syntax that feels more natural based on their preferences.

Programming Interface: listDatabases Command

For scenarios requiring database list retrieval in scripts or applications, MongoDB provides the more powerful listDatabases database command. This command is invoked through JavaScript API and offers richer output information and more flexible query options.

Basic Usage

To use the listDatabases command, it must be executed in the admin database context:

> db.adminCommand({ listDatabases: 1 })
{
  "databases": [
    { "name": "admin", "sizeOnDisk": 83886080, "empty": false },
    { "name": "local", "sizeOnDisk": 83886080, "empty": false },
    { "name": "test", "sizeOnDisk": 83886080, "empty": false }
  ],
  "totalSize": 251658240,
  "totalSizeMb": 251,
  "ok": 1
}

Database Names Only

If only database names are needed without size information, use the nameOnly parameter:

> db.adminCommand({ listDatabases: 1, nameOnly: true })
{
  "databases": [
    { "name": "admin" },
    { "name": "local" },
    { "name": "test" }
  ],
  "ok": 1
}

Permission Control and Security Considerations

In environments with authentication enabled, the behavior of the listDatabases command varies based on user privileges:

Advanced Filtering Capabilities

The listDatabases command supports using filters to screen returned databases. For example, returning only databases whose names start with a specific prefix:

> db.adminCommand({ 
    listDatabases: 1, 
    filter: { "name": /^test/ } 
  })
{
  "databases": [
    { "name": "test", "sizeOnDisk": 83886080, "empty": false }
  ],
  "totalSize": 83886080,
  "totalSizeMb": 80,
  "ok": 1
}

Special Handling in Sharded Cluster Environments

When executing the listDatabases command in sharded cluster environments, the output includes additional sharding information:

Command Selection Recommendations

Based on different usage scenarios, appropriate command selection is recommended:

Error Handling and Important Notes

When executing database listing queries, the following considerations are important:

Conclusion

MongoDB provides multiple flexible ways to list databases, ranging from simple show dbs to feature-rich listDatabases command, meeting requirements across different scenarios. Understanding the characteristics and applicable scenarios of these commands helps developers perform database management and monitoring more efficiently.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.