How to List All Collections in the MongoDB Shell?
Last Updated :
05 Mar, 2024
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:
- Using Show collections
- Utilizing db.getCollectionNames()
- Using db.getCollectionInfos()
- Using db.runCommand()
- 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
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:
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:
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:
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 collectionsConclusion
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.
Similar Reads
How to List all Databases in the Mongo Shell?
Knowing how to list databases in MongoDB is an important part of managing your data effectively. By using basic MongoDB shell commands, you can easily see what databases you have and understand their sizes. By using commands such as show dbs and db.stats() and users can gain valuable insights into t
4 min read
How to List all Users in the Mongo Shell
In MongoDB, user management is an essential aspect of database administration, allowing administrators to control access and permissions for different users. The Mongo Shell provides a powerful interface to interact with MongoDB including managing users. In this article, we'll explore how to list al
3 min read
How to Rename Collection in MongoDB?
Renaming collections in MongoDB is a straightforward operation that helps in maintaining and organizing databases efficiently. MongoDB provides the renameCollection command and the db.collection.renameCollection() method for changing collection names within the same database.In this article, We will
3 min read
What is a collection in MongoDB?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. Thi
4 min read
How to use TTL collections in MongoDB?
TTL (Time-To-Live) collections in MongoDB is a special collection where it allows automatic deletion of documents after a specified duration. This technique is used in managing temporary data, such as user sessions cache logsHow TTL WorksTTL Index: You create a TTL index on a date field in your coll
5 min read
How to add range in the Collection of Mongodb using Node.js ?
Mongoose.module is one of the most powerful external module of the node.js.Mongoose is a MongoDB ODM i.e (Object database Modelling) that used to translate the code and its representation from MongoDB to the Node.js server.Mongoose module provides several functions in order to manipulate the documen
2 min read
How to Connect to MongoDB Atlas Using Shell?
MongoDB is a highly scalable NoSQL database, renowned for its ability to manage vast amounts of complex and unstructured data. Unlike traditional databases that use a tabular format, MongoDB employs a document-oriented approach, storing data in a flexible, JSON-like format. This makes MongoDB highly
5 min read
How to create new Collection in MongoDB using Node.js ?
MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for
1 min read
How to drop collection in MongoDb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to get the size of a collection using Node.js ?
The collection.countDocuments() is the method of the mongodb module in the node.js that is used to count the total number of documents present in the collection of the particular database in the MongoDB. Syntax: collection.countDocuments(Query)Parameters: This method takes the following one paramete
1 min read