MongoDB: 3 ways to view all collections in a database

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

Introduction

Viewing collections within a MongoDB database is a fundamental task for database administrators and developers alike. MongoDB offers several ways to list all collections within a database, helping users manage their data effectively. This article will cover a variety of methods with detailed instructions.

Using the Mongo Shell

The most straightforward way to view the collections in a MongoDB database is to use the ‘show collections’ command within the Mongo Shell. This returns a list of collection names within the active database.

  1. Start the Mongo Shell and connect to your MongoDB instance.
  2. Select the database you want to view collections for by running the ‘use ‘ command.
  3. Execute the ‘show collections’ command to list all collections.

Eample:

use myDatabase
show collections

Output:

collection1
collection2
collection3

Notes: The ‘show collections’ command in the Mongo Shell is a convenient and quick way to get the list of collections. It can be run on both local and remote instances of MongoDB, and it requires minimal system resources.

Using MongoDB Compass

MongoDB Compass is the official GUI for MongoDB. It allows users to visually explore their databases and it includes a straightforward way to view collections.

  1. Open MongoDB Compass and connect to your MongoDB instance.
  2. Select the database for which you want to view collections.
  3. The UI will display the collections in the selected database.

Notes: MongoDB Compass is user-friendly and beneficial for those who prefer graphical interfaces over command-line operations. It, however, might be less convenient for automating tasks or when working on headless servers without a GUI.

Using MongoDB Drivers in an Application

Application developers working with MongoDB can programmatically list all collections in a database using MongoDB’s drivers for various programming languages, such as Node.js, Python, Java, etc.

  1. Establish a connection to your MongoDB instance using the appropriate driver in your programming language of choice.
  2. Use the ‘listCollections’ method on the database object to retrieve collection information.
  3. Iterate over the result set and output the names of collections.

Code example (Node.js):

// Assuming Node.js with the MongoDB native driver
const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017', (err, client) => {
  const db = client.db('myDatabase');

  db.listCollections().toArray(function(err, collInfos) {
    collInfos.forEach(function(coll) {
      console.log(coll.name);
    });
  });
});

Output:

collection1
collection2
collection3

Notes: Using drivers provides full control over the process and can integrate with applications for automated collection listing. However, it does require some knowledge of programming and potentially asynchronous operations, as seen in the Node.js example.

Conclusion

In summary, MongoDB offers various ways to list all collections within a database. From the simplicity and ease of use offered by the MongoDB Shell to the visually rich MongoDB Compass, and the powerful programmatic approaches provided by MongoDB drivers — there’s a solution for different needs and preferences. When selecting a method, consider factors like the environment of your MongoDB server, your familiarity with coding, and the requirements of your project to choose the most appropriate approach.