How to display databases with MongoDB List Databases
While there are a few alternatives you can use to list databases, the MongoDB List Databases offers users a variety of parameters to customize their search queries.
List Databases and its alternatives
Although MongoDB works differently than relational database management systems, such as MySQL, both types of systems require a good overview of all operations and data for users to be able to optimally manage their information. With good management practices in mind, the NoSQL solution has many different MongoDB commands that let you look up and check data stored within the database.
The MongoDB List Collections command lets you pull up a list of collections. However, if you want to look at databases and not collections, you’ll need the list databases command. In addition to providing the names of the different databases, the latter command also displays important information pertaining to available storage capacity and specifications. If you don’t want to use the MongoDB List Databases command, you can also opt to use one of the alternatives.
Set up and filtering the list databases command
The list databases command gives you all available databases along with some basic statistics. This gives you an overview and lets you know if you should create additional databases using MongoDB Create Database or remove them using the MongoDB Drop Database. Here is the syntax for the command:
db.adminCommand ( { listDatabases: 1 } )
List databases also has four additional parameters, all of which are optional. By using these parameters, you can generate a list with more precise information. These parameters are:
- filter: This allows you to carry out a more detailed search. You can use this option to remove certain databases from your query or you can also filter your search results with name, sizeOnDisk, empty and shards.
- nameOnly: nameOnly is a Boolean value. By using this option, you can choose between only having the name of the database displayed or allowing additional information to be displayed as well.
- authorizedDatabases: authorizedDatabases is also a Boolean value. This option allows you to restrict database access to authorized users. With this parameter, you can limit access to the complete overview of all databases as well.
- comment: You can also comment on the command. However, the comment must have a suitable BSON format.
List Databases example
To best understand how the list databases command and its parameters work, let’s take a look at an example. Let’s imagine we have a company database. If you enter this command without any further specifications, you will get the following output:
db.adminCommand({listDatabases: 1})
{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : <size>,</size>
"empty" : false
},
{
"name" : "team",
"sizeOnDisk" : <size>,</size>
"empty" : false
},
{
"name" : "clients_unitedstates",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"name" : "clients_france",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"totalSize" : <totalsize></totalsize>
"totalSizeMb" : <totalsizemb>,</totalsizemb>
"ok" : 1
}
List databases with nameOnly
If you change nameOnly to “true”, then the list databases output will look like this:
db.adminCommand({listDatabases: 1, nameOnly: true})
{
"databases" : [
{
"name" : "admin",
},
{
"name" : "team",
},
{
"name" : "clients_unitedstates",
}
{
"name" : "clients_france",
}
],
"ok" : 1
}
List databases with filter
If you are using a lot of databases, it can be useful to further specify your search criteria. In the following example, we’ll only display databases that begin with the letters “cli”:
>db.adminCommand({listDatabases: 1, filter: {"name": /^cli/}})
{
"databases" : [
{
"name" : "clients_unitedstates",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"name" : "clients_france",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"totalSize" : <totalsize></totalsize>
"totalSizeMb" : <totalsizemb>,</totalsizemb>
"ok" : 1
}
Alternatives to MongoDB List Databases
Alongside list databases, there are also other options to display available databases in MongoDB.
Option 1: show dbs and show databases
A very simple method to get a quick overview of your databases is MongoDB Show Dbs. You can use it to list all available MongoDB databases:
>show dbs
admin 0.007GB
team 0.013GB
clients_unitedstates 0.053GB
clients_france 0.027GB
The show databases command works like this:
>show databases
admin 0.007GB
team 0.0013GB
clients_unitedstates 0.053GB
clients_france 0.027GB
If the databases do not contain documents, the commands will not be able to identify and display them.
Option 2: get.DBNames
The method db.getMongo().getDBNames() is another good alternative to the list databases command. This method doesn’t provide additional information, but if you are only trying to generate an overview, it’s a good option. The output for this method looks like this:
>db.getMongo().getDBNames()
[
"admin",
"team",
"clients_unitedstates",
"clients_france"
]