In addition to the MongoDB List Databases command, there are alternatives for listing databases. It is also possible to specify List Databases with different parameters.
List Databases and its alternatives
While MongoDB differs from traditional database management systems like MySQL in how it works, the NoSQL solution requires a good overview of operations and data for optimal management. This is why the system offers various MongoDB commands to control the deposited documents.
The MongoDB List Collections command lists collections, while List Databases lists databases. This last command not only returns the names of the different databases, it also provides valuable information about memory usage and settings. If you prefer to use other MongoDB commands than List Databases, there are alternatives depending on your intended use.
Structure and filter of the List Databases command
The List Databases command displays all available databases along with some basic statistics. This command provides an overview of whether you need to create additional databases with the MongoDB Create Database command or delete them with the MongoDB Drop Database command. The command syntax is as follows:
db.adminCommand ( { listDatabases: 1 } )
Additionally, List Databases has four optional parameters to further filter a list:
- filter: makes it possible to carry out an even more precise search. Use this option to filter your search results and thus exclude certain databases from the outset. Allowed filter parameters are name, sizeOnDisk, empty, and shards;
- nameOnly: boolean value (true or false), this option defines whether only the database names should be displayed or whether additional information should also be indicated;
- authorizedDatabases: Boolean value, used to define the databases displayed according to the different users. Thus, it is for example possible to exclude unauthorized persons from having access to databases;
- how : allows inserting a command comment in any suitable BSON format.
Examples of List Databases
The best way to understand how the List Databases command and its parameters work is with an example. For this, imagine the databases of a company. If you enter the corresponding command without further specificationhere is the output:
db.adminCommand({listDatabases: 1})
{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : <size>,</size>
"empty" : false
},
{
"name" : "team",
"sizeOnDisk" : <size>,</size>
"empty" : false
},
{
"name" : "clients_france",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"name" : "clients_allemagne",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"totalSize" : <totalsize></totalsize>
"totalSizeMb" : <totalsizemb>,</totalsizemb>
"ok" : 1
}
List Databases with nameOnly
If you change the nameOnly value to « true », this is what the List Databases output looks like:
db.adminCommand({listDatabases: 1, nameOnly: true})
{
"databases" : [
{
"name" : "admin",
},
{
"name" : "team",
},
{
"name" : "clients_france",
}
{
"name" : "clients_allemagne",
}
],
"ok" : 1
}
List Databases with Filter
If you are using a large number of databases, it may be useful to specify your search criteria from the List Databases command. In the following example, we are looking to display only databases that start with the letters “cli”:
>db.adminCommand({listDatabases: 1, filter: {"name": /^cli/}})
{
"databases" : [
{
"name" : "clients_france",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"name" : "clients_allemagne",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"totalSize" : <totalsize></totalsize>
"totalSizeMb" : <totalsizemb>,</totalsizemb>
"ok" : 1
}
Alternatives to List Databases
Besides List Databases, MongoDB offers other possibilities to display available databases.
Option 1: show dbs and show databases
A very simple method to quickly list your databases is the MongoDB show dbs command. It displays all available MongoDB databases:
>show dbs
admin 0.007GB
team 0.013GB
clients_france 0.053GB
clients_allemagne 0.027GB
The command Show databases works the same way:
>show databases
admin 0.007GB
team 0.0013GB
clients_france 0.053GB
clients_allemagne 0.027GB
Noticed
Both commands only recognize databases that contain documents.
Option 2: get.DBNames
The method db.getMongo().getDBNames() is also an alternative to the List Databases command. Under MongoDB, it certainly does not provide any additional information but is sufficient to give an overview. This is what the output looks like:
>db.getMongo().getDBNames()
[
"admin",
"team",
"clients_france",
"clients_allemagne"
]