How to use MongoDB List Collections and other list options
There are four different options to list MongoDB collections: List Collections, Show Collections, db.getCollectionNames() und db.getCollectionInfos(). The first command is the most common one and shows you all the names and options in your lists.
How to show collections as lists in MongoDB
With its noteworthy flexibility and scalability, the NoSQL database management system MongoDB is an impressive tool. Unlike MySQL, the system stores data as documents rather than in tables. You can create collections using the MongoDB Create Collection command and remove them using the MongoDB Drop Collection command.
If you would like an overview of all your collections, there are a few MongoDB commands to choose from. To display multiple collections or all collections at once, you have the choice between the list collections and show collections commands as well as the db.getCollectionNames() and db.getCollectionInfos().
The list collections command
The MongoDB List Collections command is a particularly useful way to display individual collections in their entirety or all the collections at once. It gives a complete list of names and options in individual lists and allows you to narrow down results with different parameters.
List collections syntax
The list collections command is as follows:
{listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any>}</any></boolean></boolean></document>
The four optional parameters in List Collections are:
- filter: This allows you to adapt your search query.
- nameOnly: This is a Boolean value that determines if only the name of the corresponding collection is shown or if other information should be displayed as well.
- authorizedCollections: By using this Boolean value, you limit access to the list, allowing only those users who are authorized the ability to manage them.
- comment: This option allows you to mark a search query or add a more detailed description to a search query.
An example of list collections
Here’s an example of how to use the command:
db.runCommand({listCollections: 1, authorizedCollections: true, nameOnly: true})
The output is a cursor with the information that is available. Here is an example of what this looks like:
"cursor" : {
"id" : NumberLong (0),
"ns" : "clientlist.$cmd.listCollections",
"firstBatch" : [
{
"name" : "list.unitedstates",
"type" : "collection"
},
{
"name" : "list.france",
"type" : "collection"
},
{
"name" : "list.italy",
"type" : "collection"
}
]
},
"ok" : 1
}
Alternative list 1: show collections
If you simply want a quick overview of the collections in your current database, the show collections command is a good choice. You can also use this command to list MongoDB collections. To do this, open the corresponding database first and then use the show collections command. This is what the output should look like:
>use client list
>show collections
list.unitedstates
list.france
list.italy
List alternative 2: db.getCollectionNames()
If you would like a quick overview of the collections in your database, then the db.getCollectionNames() method is also suitable. This method only provides you with the names of different collections but is quite useful if your goal is to just list a few Mongo DB collections. Here is the entry and output for this:
>db.getCollectionNames();
[
"list.germany",
"list.france",
"list.italy"
]
List alternative 3: db.getCollectionInfos()
>db.getCollectionInfos();
[
{
"name" : "list.unitedstates",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : true,
"uuid" : UUID (<uuid>)</uuid>
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_iod_"
}
},