Updated March 6, 2023
Introduction to Mongodb show collections
MongoDB show collections are defined as display all collections and views from the connected database; in MongoDB, we can list all collections using the show collections command. There are various methods available to show collections in MongoDB, we can list collections by using show collections, list collections, db.getCollectionNames () and db.getCollectionInfos () methods. Using the show collections command, we can list all collections from the connected database. While working with multiple collections, we need a list of all collections same time; we can use the show collections command in MongoDB.
Syntax
Below is the syntax of MongoDB show collections are as follows. We can show collections by using the following method.
- Using show collections command.
show collections
- Using list collections command.
db.runCommand ({listCollections: <value>, nameOnly: <value>, authorizedCollections: <value>})
- Using getCollectionNames method.
db.getCollectionNames ()
- Using getCollectionInfos method.
db.getCollectionInfos (filter (filter collection), nameOnly (Method only return name of collection.), authorized collections (valid value is true or false))
Parameter description of show collections command –
- Show collections – This method is used in MongoDB to list all the collections and view names from the connected database.
- List collections – This method is used to list the collection names and all the details in MongoDB. Using this method, we can use multiple parameters to display the details of collections.
- Name only – This parameter is used in list collections and the getCollectionInfos method. This parameter is used to display the name of the specified collection.
- Authorized collections – This parameter is used in list collections and the getCollectionInfos method. The valid value of this parameter is true or false; if this value is set as true, then it allowing the user without any required privileges.
- GetCollectionNames – This method is used in MongoDB to get all the collection names from the specified database in MongoDB.
- GetCollectionInfos – This method is used to retrieve all the collection names with details from the connected database.
How to show collections in Mongodb?
- In MongoDB, we can show collections and views using a different method. Below is the method which was used to show collections.
- Show collections
- List collections
- getCollectionNames ()
- getCollectionInfos ()
- To get only the name of the collection and views show collections is the best command to show all collections from the database.
- To list all the collections and views with details, then we can use the list collections command in MongoDB.
- Using the getCollectionNames command, we can only list all the collection names in format.
- If we have to find all the collection names with details and specified collection names with details, then we can use the getCollectionInfos method in MongoDB.
- To list the collection of specified databases, we need to first connect to that database; after connecting to the database, we can list all the collections and views. Without connecting to the specified database, it’s not possible to show the collections.
- The below example shows that we need to connect the specified database to show the collection.
use test
show collections
Figure – we need to connect the specified database to show the collection.
- In the above example, we have first connected to the test database and then used the show collections command to list the collections from the test database.
- We can list only those collections on which we have access; without access, we can’t list the collection from the database.
Example
Here are the following examples mentioned below
1 Show collection using show collections command
- The below example shows show all the collections using the show collections command.
- We have listed all the collections from the test database. Before using this command, we have connected to the test database. The test database contains the 5 collections in it.
Code:
use test
show collections
Figure – Show collection using show collections command.
2. Show collection using list collections command
- The below example shows show collection by using the list collection command. We have used authorized collection and name the only parameter with the list collection command.
- In the below example, we list the collection from the coll_test database. Coll_test database contains only one collection, so it will show detailed info of collection_test collection.
- In the below image, we can see it will show all details info like name of the collection, type, etc.
Code:
use coll_test
db.runCommand ({listCollections: 1.0, authorizedCollections: true, nameOnly: true })
Figure – Show collection using the list collections command in MongoDB.
3. Show collection using getCollectionNames method
- The below example shows a list of the collection by using the getCollectionNames method.
- In the below example, we have list the collection from the test database. The test database contains five collections, so it will show all five collection names in format.
- In the below image, we can see that the output only shows the name of the collection which is present in the test collection.
Code:
use test
db.getCollectionNames ()
Figure – Show collection using getCollectionNames method in MongoDB.
4. Show collection using getCollectionInfos method
- The below example shows show collection by using the getCollectionInfos method. In the first example, we have used authorized collection, filter, and name, the only parameter with the getCollectionInfos method.
- In the below example, we list the collection from the coll_test database. Coll_test database contains only one collection, so it will show detailed info of collection_test collection.
Code:
use coll_test
db.getCollectionInfos ({}, true, true)
Figure – Show collection using the getCollectionInfos method using a parameter.
- In the below example, we have not used any parameter with the getCollectionInfos method.
Code:
use coll_test
db.getCollectionInfos ({}, true, true)
Figure – Show collection using the getCollectionInfos method, excluding the parameter.
5. Show single collection details using the getCollectionInfos method
- In the below example, we have used the collection name using the getCollectionInfos method.
- We have used collection_test collection to display single collection details. With the getCollectionInfos method, we have used all the parameters.
Code:
use coll_test
db.getCollectionInfos ({name: "collection_test"}, true, true)
Figure – Show single collection details using getCollectionInfos method in MongoDB.
Conclusion
Show collection command is used to list all the collections and views from connected databases. We can use show collections, list collections getCollectionInfos, and getCollectionNames method to show all the collections from the connected database. To list the view and collection, first, we need to connect the specified database.
Recommended Articles
This is a guide to Mongodb show collections. Here we discuss How to show collections in Mongodb along with the Parameter description of the show collections command. You may also have a look at the following articles to learn more –