While there are a few al­tern­at­ives you can use to list databases, the MongoDB List Databases offers users a variety of para­met­ers to customise their search queries.

List Databases and its al­tern­at­ives

Although MongoDB works dif­fer­ently than re­la­tion­al database man­age­ment systems, such as MySQL, both types of systems require a good overview of all op­er­a­tions and data for users to be able to optimally manage their in­form­a­tion. With good man­age­ment 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 Col­lec­tions command lets you pull up a list of col­lec­tions. However, if you want to look at databases and not col­lec­tions, you’ll need the list databases command. In addition to providing the names of the different databases, the latter command also displays important in­form­a­tion per­tain­ing to available storage capacity and spe­cific­a­tions. If you don’t want to use the MongoDB List Databases command, you can also opt to use one of the al­tern­at­ives.

Set up and filtering the list databases command

The list databases command gives you all available databases along with some basic stat­ist­ics. This gives you an overview and lets you know if you should create ad­di­tion­al 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 ad­di­tion­al para­met­ers, all of which are optional. By using these para­met­ers, you can generate a list with more precise in­form­a­tion. These para­met­ers 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 ad­di­tion­al in­form­a­tion to be displayed as well.
  • au­thor­ized­Data­bases: au­thor­ized­Data­bases is also a Boolean value. This option allows you to restrict database access to au­thor­ised 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 un­der­stand how the list databases command and its para­met­ers 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 spe­cific­a­tions, you will get the following output:

db.adminCommand({listDatabases: 1})
{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : <size>,
"empty" : false
},
{
"name" : "team",
"sizeOnDisk" : <size>,
"empty" : false
},
{
"name" : "clients_unitedkingdom",
"sizeOnDisk" : <size>,
"empty" : false
}
],
"name" : "clients_france",
"sizeOnDisk" : <size>,
"empty" : false
}
],
"totalSize" : <totalsize>
"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_unitedkingdom",
}
{
"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_unitedkingdom",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"name" : "clients_france",
"sizeOnDisk" : <size>,</size>
"empty" : false
}
],
"totalSize" : <totalsize></totalsize>
"totalSizeMb" : <totalsizemb>,</totalsizemb>
"ok" : 1
}

Al­tern­at­ives 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_unitedkingdom 0.053GB
clients_france 0.027GB

The show databases command works like this:

>show databases
admin 0.007GB
team 0.0013GB
clients_unitedkingdom 0.053GB
clients_france 0.027GB
Note

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().get­DB­Names() is another good al­tern­at­ive to the list databases command. This method doesn’t provide ad­di­tion­al in­form­a­tion, 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_unitedkingdom",
"clients_france"
]
Go to Main Menu