How to List All Databases in MongoDB Shell

Tahseen Tauseef Feb 02, 2024
  1. Introduction to Mongo Shell
  2. Create a MongoDB Database and Collection
  3. Insert a Document Into a MongoDB Collection
  4. List the Databases in Mongo Shell
  5. List the Default Databases Using MongoDB
  6. Use Mongo CLI to List Databases as a JSON Response
How to List All Databases in MongoDB Shell

This article tackles how to list your databases in Mongo shell. Moreover, how you can use Mongo CLI.

Introduction to Mongo Shell

It’s critical to learn Mongo Shell if you’re going to store data in MongoDB. This command-line interface (CLI) is a JavaScript-based interactive tool for querying and even modifying MongoDB collections and their BSON documents.

Before going on to the examples in this tutorial, make sure your computer has a MongoDB server installed and operating. Once MongoDB is running, the user can initialize the MongoDB status by starting a Mongo Shell instance at the console.

Run the following command to get the Mongo Shell CLI’s version number.

Command:

mongo --version

In the next section, you will start utilizing Mongo Shell to query and perform database operations in MongoDB. You may use this interactive command-line interface to accomplish various activities, such as listing all of our databases.

Type mongo on the command line to access the client interface. You can use the -verbose option to get more detailed feedback on the commands you run in the shell.

Command:

mongo--verbose

The user can also connect to the Mongo Shell by directly connecting to the port process, as shown in the sample below. Use the -u and -p flags to pass username and password credentials to the command.

Command:

mongo -u USER_NAME -p pAsSwOrD_123

This command creates an instance from which Mongo Shell will be launched. You can connect to MongoDB and begin working with it once you’ve successfully entered the shell interface.

Create a MongoDB Database and Collection

You can now use the Mongo Shell to access a database namespace by typing 'use'.

Command:

use testDb

Output:

Switched to db testDb

You can also look at your current database with the db command. When you’re through with a database, use the logout() method of the db object to disconnect from it.

Insert a Document Into a MongoDB Collection

As you insert documents into MongoDB, it will generate a collection on the fly.

Query:

db.testCollection.insert({ "hello" : "world" })

The response nInserted should indicate the number of inserted documents.

Output:

WriteResult({ "nInserted" : 1 })

List the Databases in Mongo Shell

To list the databases on the server, execute the following command given below in Mongo Shell.

Command:

show dbs

Output:

admin   0.000GB
config  0.000GB
local   0.000GB

The user can also use the show command to see a list of all the collections on the MongoDB server.

Show collections

List the Default Databases Using MongoDB

The Mongo Shell returns all of the default MongoDB databases as well as any of your databases if you call the getMongo().getDBNames() method on the database (db) object.

Query:

db.getMongo().getDBNames()

Output:

[ "admin", "config", "local" ]

Use Mongo CLI to List Databases as a JSON Response

Running a specific command that executes an admin command is another technique to list databases in the Mongo Shell. This will provide more database information in a human-readable JSON format.

Query:

db.adminCommand('listDatabases')

Output:

{
    "databases" : [
        {
            "name" : "admin",
            "sizeOnDisk" : 40960,
            "empty" : false
        },
        {
            "name" : "config",
            "sizeOnDisk" : 98304,
            "empty" : false
        },
        {
            "name" : "local",
            "sizeOnDisk" : 73728,
            "empty" : false
        }
    ],
    "totalSize" : 212992,
    "ok" : 1
}

Returning the result in JSON format lets you see more detailed information about the databases when using Mongo Shell.

It’s important to know which databases are on your server when dealing with MongoDB. Fortunately, the interactive Mongo Shell provides several options for obtaining this data.

This article demonstrated a couple of different approaches to listing databases in the Mongo Shell.