MongoDB

Mongo Shell List All Databases

Mongo Shell is a powerful command-line interface for managing and administering your MongoDB databases. It provides an intuitive and interactive command line utility that allows you to manage the databases, collections, and individual documents.

Therefore, learning how to use the Mongo Shell is critical for any MongoDB user. In this tutorial, we will help you master the Mongo Shell commands by discussing how to view all the databases in your server.

Let’s jump in.

Requirements

Before proceeding, it is good to ensure that you met the following requirements:

  1. MongoDB Server
  2. Mongo Shell tools

With the given requirements met, we can proceed.

Mongo Shell List Database Collection – Method 1

The most common method of viewing the available databases in a MongoDB Server is using the show dbs command.

Log-in into your Mongo Shell:

$ mongosh

Once logged in, run the following command to show all the databases in the server:

test> show dbs

The command should return all the databases in the server and their corresponding database size as shown in the following sample output:

admin       40.00 KiB
config      72.00 KiB
customers    1.27 MiB
film       236.00 KiB
local       72.00 KiB
store       88.00 KiB
test       244.00 KiB

You can use the longer version of the command as:

$ show databases;

Output:

Mongo Shell List Databases – listDatabases Command

Another technique that we can use to show the list of all the databases in the server is using the Mongo Shell admin command. The command returns the detailed information about the databases in the server as JSON object.

The command syntax is as shown in the following:

db.adminCommand(
   {
     listDatabases: 1
   }
)

The command should return an output as follows:

{
  databases: [
    { name: 'admin', sizeOnDisk: Long("40960"), empty: false },
    { name: 'config', sizeOnDisk: Long("98304"), empty: false },
    { name: 'customers', sizeOnDisk: Long("1335296"), empty: false },
    { name: 'film', sizeOnDisk: Long("241664"), empty: false },
    { name: 'local', sizeOnDisk: Long("73728"), empty: false },
    { name: 'store', sizeOnDisk: Long("90112"), empty: false },
    { name: 'test', sizeOnDisk: Long("249856"), empty: false }
  ],
  totalSize: Long("2129920"),
  totalSizeMb: Long("2"),
  ok: 1
}

The command returns the database name, the size taken on the disk, and whether or not the database contains the collections or not. Keep in mind that the previous command returns the databases in ascending order.

Mongo Shell List Databases – getMongo()

We can also use the getMongo() function to show the available databases in the server.

The command is as shown:

db.getMongo().getDBNames()

This should return the names of the databases in the server as follows:

[ 'admin', 'config', 'customers', 'film', 'local', 'store', 'test' ]

Conclusion

In this post, we discussed the various methods and techniques of fetching the available databases in your MongoDB Server.

Thanks for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list