Sling Academy
Home/MongoDB/3 Ways to List All Databases in MongoDB

3 Ways to List All Databases in MongoDB

Last updated: February 01, 2024

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.

Next Article: MongoDB Connection String URI Format: Explained with Examples

Previous Article: 3 ways to create a new database in MongoDB

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)