Open In App

How to List All Collections in the MongoDB Shell?

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Managing collections is a fundamental task in MongoDB database administration. Knowing how to list all collections in the MongoDB shell is essential for understanding your database structure and managing your data effectively. In this article, we’ll explore how to list all collections in the MongoDB shell and discuss why this knowledge is important for database administrators and developers.

How to List All Collections in the Mongo shell?

When working with MongoDB it’s important to be able to list all collections in a database. This is particularly important for database administrators and developers who need to understand the database’s structure and manage its collections efficiently. Below are the methods that help to list all collections in Mongo shell are as follows:

  1. Using Show collections
  2. Utilizing db.getCollectionNames()
  3. Using db.getCollectionInfos()
  4. Using db.runCommand()
  5. Using Show dbs and Show Collections Together

Let’s understand each method with the help of examples

1. Using Show Collections

We can easily list all collections in the current database by executing the show collections command in the MongoDB shell.

This command provides us with a straightforward list of non-system collections within the database we’re currently working in.

Example

We listed all collections in the current database with show collections, to obtain a clear view of its structure.

show collections

Output:

show-collections

show collections

2. Utilizing db.getCollectionNames()

Another method we can use is db.getCollectionNames(), which returns an array of collection names in the current database.

By running this command, we receive an array containing the names of all collections present in the database we’re currently connected to.

Example

We retrieved an array of collection names in the current database using db.getCollectionNames(), providing us with a quick overview of available collections.

db.getCollectionNames()

Output:

dbgetCollectionNames()

db.getCollectionNames()

3. Using db.getCollectionInfos()

Similarly, we can gather detailed information about collections using the db.getCollectionInfos() method.

When executed, this method returns an array of documents, each containing information about a specific collection, including its name, type, and options.

Example

Here, We gathered detailed information about collections in the current database via db.getCollectionInfos(), obtaining specifics like names, types, and options for each collection.

db.getCollectionInfos()

Output:

dbgetCollectionInfos()

db.getCollectionInfos()

4. Using db.runCommand()

The db.runCommand() method is used to execute various database commands, including listing collections. The listCollections command retrieves information about collections and views in the current database.

We use the listCollections command with the appropriate parameters ({ listCollections: 1, nameOnly: true }), and we obtain a cursor object containing details about collection names and types in the current database.

Example:

We executed the listCollections command with db.runCommand(), acquiring a cursor object containing collection names and types in the current database.

db.runCommand({ listCollections: 1, nameOnly: true })

Output:

dbrunCommand()

db.runCommand()

5. Using Show dbs and Show Collections Together

Combining show dbs and show collections commands allows us to list collections across all databases accessible in the MongoDB instance.

First, we use show dbs to display all available databases, then we switch to a specific database using use database_name, and finally, we execute show collections to list its collections.

Example

We combined show dbs and show collections commands to list collections across accessible databases. Starting with show dbs, we navigated to a specific database and listed its collections.

show dbs
use database_name
show collections

Output:

show-dbs-and-show-collections

show dbs and show collections

Conclusion

Overall, being able to list all collections in MongoDB is essential for effective database management. The methods discussed in this article, such as show collections, db.getCollectionNames(), and db.getCollectionInfos(), provide valuable insights into the database structure and help manage collections efficiently. These methods allow database administrators and developers to easily access and analyze their data, leading to better performance optimization and troubleshooting.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads