
There are four different methods to list its collections in MongoDB: List Collections, Show Collections, db.getCollectionNames() And db.getCollectionInfos(). The first is the most used. It displays all the names and options of each of the lists.
Show MongoDB Collections as a List
The NoSQL MongoDB database management system is characterized by its great flexibility and scalability. Unlike MySQL, the system does not use arrays but stores data as documents in collections. It is thus possible to create these collections with the MongoDB Create Collection command and to delete them with the MongoDB Drop Collection command.
The system provides several MongoDB commands to display a overview of all collections. If you want to display some of them, or all the collections, use the commands List Collections, Show Collections or the methods db.getCollectionNames() And db.getCollectionInfos().
The List Collections command
The MongoDB List Collections command is a very convenient way to display all collections or a portion of them. She displays all names and options of the different lists and can be specified by several parameters.
Syntax of List Collections
Here is how the List Collections command is constructed:
{listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any>}</any></boolean></boolean></document>
The four optional parameters of List Collections are:
- filter : adapts the search query;
- nameOnly : Boolean value which determines whether only the name of the collection concerned must be displayed or whether additional information must also be provided;
- authorizedCollections : Boolean value which limits access to the list and authorizes it only to users who have the rights;
- how : Allows you to add a more detailed description or tag of the search query.
Example of using List Collections
The command is used as in the example below:
db.runCommand({listCollections: 1, authorizedCollections: true, nameOnly: true})
The output is then a slider with the available information. As an example, this is what it looks like:
"cursor" : {
"id" : NumberLong (0),
"ns" : "listeclients.$cmd.listCollections",
"firstBatch" : [
{
"name" : "liste.france",
"type" : "collection"
},
{
"name" : "liste.france",
"type" : "collection"
},
{
"name" : "liste.italie",
"type" : "collection"
}
]
},
"ok" : 1
}
Alternate 1: Show Collections
To get a quick overview of the collections in your current database, use the Show Collections command. This command also allows you to list MongoDB collections. For this you first call the corresponding database and then request all collections. This is what the output looks like:
>use listeclients
>show collections
liste.france
liste.allemagne
liste.italie
Alternate 2: db.getCollectionNames()
For a quick overview of the collections in your database, you can also use the method db.getCollectionNames(). This only returns the names of the different collections and is particularly useful if you just want to list some MongoDB collections. Here’s how the command and output look like:
>db.getCollectionNames();
[
"liste.france",
"liste.allemagne",
"liste.italie"
]
Alternate 3: db.getCollectionInfos()
The method db.getCollectionInfos() allows to have a precise overview on the list of MongoDB collections. Besides the name, it also provides all relevant additional information. For the first collection, this is what it looks like with our example:
>db.getCollectionInfos();
[
{
"name" : "liste.france",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : true,
"uuid" : UUID (<uuid>)</uuid>
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_iod_"
}
},