Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
databasesarray containing database information - Each database object includes
name,sizeOnDisk, andemptyfields - The
totalSizefield 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.
Advertisements
