3 Ways to List All Databases in MongoDB

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Managing data effectively requires an understanding of the database environment you’re working with. In MongoDB, it’s often necessary to list all the databases to understand the scope of the data managed. This guide will explore various methods to accomplish this task.

Approach 1: Using the mongo Shell

The mongo shell is an interactive JavaScript interface to MongoDB. It provides a rich set of methods to interact with your database. One simple yet common approach for listing databases is through the ‘show dbs’ command within the mongo shell.

  • Step 1: Open your terminal or command prompt.
  • Step 2: Enter the mongo shell by typing ‘mongo’ and hitting enter.
  • Step 3: List all databases by running the command ‘show dbs’.

Example:

show dbs

Output (may vary):

admin 0.000GB
test 0.002GB
userDB 0.005GB

Notes: This method is straightforward and doesn’t require any additional programming, making it Ideal for quick checks. However, it requires shell access and may not be suitable for automated scripts.

Approach 2: Using MongoDB Compass

MongoDB Compass is the official GUI for MongoDB. It allows users to visualize and manipulate their data and offers an intuitive interface for listing databases.

  • Step 1: Download and install MongoDB Compass from the official MongoDB website.
  • Step 2: Connect to your MongoDB instance by entering your connection details.
  • Step 3: Upon successful connection, you will be presented with a list of all databases in the ‘Databases’ tab.

Notes: Compass does not require any scripting, making it user-friendly. However, it is not a CLI tool and thus not ideal for environments where GUI access is restricted.

Approach 3: Using MongoDB Database command

The MongoDB ‘db.adminCommand()’ function can be used to list databases programmatically. By passing the ‘listDatabases’ command, we can retrieve a list of all databases on the server.

  • Step 1: Connect to your MongoDB instance using a MongoDB driver.
  • Step 2: Access the ‘admin’ database.
  • Step 3: Use the ‘db.adminCommand({ listDatabases: 1 })’ to get a list of the databases.
  • Step 4: Print or manipulate the returned list as needed.

Code example (Node.js):

// Connect to the MongoDB instance
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';

MongoClient.connect(url, function(err, client) {
    console.log("Connected successfully to server");
    const adminDb = client.db('admin');

    // List all the available databases
    adminDb.adminCommand({ listDatabases: 1 }, function(err, databases) {
        console.log(databases);
        client.close();
    });
});

Notes: This method is suitable for scripting and automation. Using the data returned by the command entails working with JSON objects, which is typically convenient for developers. However, it requires a programming environment set up.

Conclusion

Listing databases in MongoDB can be performed through various methods, each suited for particular needs. The mongo shell is excellent for a quick check, MongoDB Compass provides a visually rich experience, and the db.adminCommand() for scripting connection via programming languages. Your choice depends on whether you prefer a command-line approach, graphical interface, or require an automated script to list databases.