There are four different options to list MongoDB col­lec­tions: List Col­lec­tions, Show Col­lec­tions, db.get­Col­lec­tion­Names() und db.get­Col­lec­tion­In­fos(). The first command is the most common one and shows you all the names and options in your lists.

How to show col­lec­tions as lists in MongoDB

With its note­wor­thy flex­i­bil­i­ty and scal­a­bil­i­ty, the NoSQL database man­age­ment system MongoDB is an im­pres­sive tool. Unlike MySQL, the system stores data as documents rather than in tables. You can create col­lec­tions using the MongoDB Create Col­lec­tion command and remove them using the MongoDB Drop Col­lec­tion command.

If you would like an overview of all your col­lec­tions, there are a few MongoDB commands to choose from. To display multiple col­lec­tions or all col­lec­tions at once, you have the choice between the list col­lec­tions and show col­lec­tions commands as well as the db.get­Col­lec­tion­Names() and db.get­Col­lec­tion­In­fos().

The list col­lec­tions command

The MongoDB List Col­lec­tions command is a par­tic­u­lar­ly useful way to display in­di­vid­ual col­lec­tions in their entirety or all the col­lec­tions at once. It gives a complete list of names and options in in­di­vid­ual lists and allows you to narrow down results with different pa­ra­me­ters.

List col­lec­tions syntax

The list col­lec­tions command is as follows:

{listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any>}</any></boolean></boolean></document>

The four optional pa­ra­me­ters in List Col­lec­tions are:

  • filter: This allows you to adapt your search query.
  • nameOnly: This is a Boolean value that de­ter­mines if only the name of the cor­re­spond­ing col­lec­tion is shown or if other in­for­ma­tion should be displayed as well.
  • au­tho­rized­Col­lec­tions: By using this Boolean value, you limit access to the list, allowing only those users who are au­tho­rized the ability to manage them.
  • comment: This option allows you to mark a search query or add a more detailed de­scrip­tion to a search query.

An example of list col­lec­tions

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 in­for­ma­tion 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
}

Al­ter­na­tive list 1: show col­lec­tions

If you simply want a quick overview of the col­lec­tions in your current database, the show col­lec­tions command is a good choice. You can also use this command to list MongoDB col­lec­tions. To do this, open the cor­re­spond­ing database first and then use the show col­lec­tions command. This is what the output should look like:

>use client list
>show collections
list.unitedstates
list.france
list.italy

List al­ter­na­tive 2: db.get­Col­lec­tion­Names()

If you would like a quick overview of the col­lec­tions in your database, then the db.get­Col­lec­tion­Names() method is also suitable. This method only provides you with the names of different col­lec­tions but is quite useful if your goal is to just list a few Mongo DB col­lec­tions. Here is the entry and output for this:

>db.getCollectionNames();
[
"list.germany",
"list.france",
"list.italy"
]

List al­ter­na­tive 3: db.get­Col­lec­tion­In­fos()

For a precise look at the MongoDB col­lec­tions list, use the db.get­Col­lec­tion­In­fos() method. This will give you the name as well as all relevant in­for­ma­tion for the col­lec­tion. Using the first col­lec­tion as an example, it would look like this:

>db.getCollectionInfos();
[
{
"name" : "list.unitedstates",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : true,
"uuid" : UUID (<uuid>)</uuid>
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_iod_"
}
},
Go to Main Menu