What is the BSON query for the command 'show dbs' (list of databases) in MongoDB?

The BSON equivalent of the show dbs command in MongoDB is the listDatabases administrative command. This command must be run against the admin database to retrieve information about all databases on the MongoDB instance.

Syntax

db.runCommand({ listDatabases: 1 })

Example

First, switch to the admin database ?

use admin
switched to db admin

Now, run the listDatabases command ?

db.runCommand({ listDatabases: 1 })

This will produce the following output ?

{
    "databases": [
        {
            "name": "admin",
            "sizeOnDisk": 2375680,
            "empty": false
        },
        {
            "name": "app",
            "sizeOnDisk": 32768,
            "empty": false
        },
        {
            "name": "business",
            "sizeOnDisk": 417792,
            "empty": false
        },
        {
            "name": "config",
            "sizeOnDisk": 106496,
            "empty": false
        },
        {
            "name": "local",
            "sizeOnDisk": 81920,
            "empty": false
        },
        {
            "name": "test",
            "sizeOnDisk": 32944128,
            "empty": false
        }
    ],
    "totalSize": 45756416,
    "ok": 1
}

Key Points

  • The command returns a databases array containing database information
  • Each database object includes name, sizeOnDisk, and empty fields
  • The totalSize field shows the combined size of all databases
  • Must be executed against the admin database for proper authorization

Conclusion

Use db.runCommand({ listDatabases: 1 }) from the admin database to get the BSON equivalent of show dbs. This returns detailed information about all databases including their sizes and status.

Updated on: 2026-03-15T02:18:07+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements