# How to use MongoDB List Collections and other list options

There are four different options to list [MongoDB](https://www.ionos.com/digitalguide/websites/web-development/mongodb-an-introduction-and-comparison-to-mysql/) 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](https://www.ionos.com/digitalguide/hosting/technical-matters/database-management-systems-dbms/ "Database Management Systems (DBMS)") MongoDB is an impressive tool. Unlike [MySQL](https://www.ionos.com/digitalguide/server/know-how/what-is-mysql/ "What is MySQL"), the system stores data as documents rather than in tables. You can create collections using the [MongoDB Create Collection command](https://www.ionos.com/digitalguide/websites/web-development/mongodb-create-collection/ "MongoDB Create Collection") and remove them using the [MongoDB Drop Collection command](https://www.ionos.com/digitalguide/websites/web-development/mongodb-drop-collection/ "MongoDB Drop Collection").

If you would like an **overview of all your collections**, there are a few [MongoDB commands](https://www.ionos.com/digitalguide/websites/web-development/mongodb-commands/ "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:

```none
{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:

```none
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:

```none
"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:

```none
>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:

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

## List alternative 3: db.getCollectionInfos()

For a precise look at the MongoDB collections list, use the *db.getCollectionInfos()* method. This will give you the name as well as all relevant information for the collection. Using the first collection as an example, it would look like this:

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


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/mongodb-list-collections/](https://www.ionos.com/digitalguide/websites/web-development/mongodb-list-collections/) for AI/LLM consumption.