4 Ways to List the Collections in a MongoDB Database

When using MongoDB, there’s more than one way to list the collections in a database.

Here are four ways to get a list of collections in a MongoDB database:

  • The show collections Command
  • The listCollections Command
  • The db.getCollectionNames() Method
  • The db.getCollectionInfos() Method

The show collections Command

If you’re using the mongo shell, the quickest way to get a list of collections is to use the show collections command. This command retrieves a list of collections and views in the current database.

Example:

show collections

Result:

 employees
 pets
 pettypes
 products
 system.views 

In this case, there are five results. We can’t tell just by looking at it, but pettypes is actually a view. The others are collections.

The system.views collection is a system collection that contains information about each view in the database.

The actual collections returned will depend on your access level:

  • For users with the required access, show collections lists the non-system collections for the database.
  • For users without the required access, show collections lists only the collections for which the users has privileges.

The listCollections Command

The listCollections administrative command returns the name and options of collections and views in the database. It returns the information in the form of a document.

Example:

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )

Result:

{
    "cursor" : {
        "id" : NumberLong(0),
        "ns" : "PetHotel.$cmd.listCollections",
        "firstBatch" : [
            {
                "name" : "employees",
                "type" : "collection"
            },
            {
                "name" : "system.views",
                "type" : "collection"
            },
            {
                "name" : "pets",
                "type" : "collection"
            },
            {
                "name" : "products",
                "type" : "collection"
            },
            {
                "name" : "pettypes",
                "type" : "view"
            }
        ]
    },
    "ok" : 1
}

The document contains information with which to create a cursor to the collection information.

This time we can see which ones are collections and which are views.

We can also run the command like this:

db.runCommand( { listCollections: 1.0 } )

Doing that provides a lot more information about the collections. See the db.getCollectionInfos() example below to see the data returned when running it like that (the db.getCollectionInfos() method is a wrapper around listCollections).

The db.getCollectionNames() Method

The db.getCollectionNames() method returns an array containing the names of all collections and views in the current database, or if running with access control, the names of the collections according to the user’s privilege.

Example:

db.getCollectionNames()

Result:

 [ "employees", "pets", "pettypes", "products", "system.views" ] 

The db.getCollectionInfos() Method

The db.getCollectionInfos() method returns an array of documents with collection or view information, such as name and options, for the current database. The results depend on the user’s privilege.

Here’s an example of calling it without any arguments:

db.getCollectionInfos()

Result:

[
    {
        "name" : "employees",
        "type" : "collection",
        "options" : {
            
        },
        "info" : {
            "readOnly" : false,
            "uuid" : UUID("07e89c25-8842-4331-a1a9-96fe0b4745dc")
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_"
        }
    },
    {
        "name" : "pets",
        "type" : "collection",
        "options" : {
            
        },
        "info" : {
            "readOnly" : false,
            "uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_"
        }
    },
    {
        "name" : "pettypes",
        "type" : "view",
        "options" : {
            "viewOn" : "pets",
            "pipeline" : [
                {
                    "$project" : {
                        "type" : 1
                    }
                }
            ]
        },
        "info" : {
            "readOnly" : true
        }
    },
    {
        "name" : "products",
        "type" : "collection",
        "options" : {
            "capped" : true,
            "size" : 7500544,
            "max" : 7000
        },
        "info" : {
            "readOnly" : false,
            "uuid" : UUID("cb084959-f374-4f51-bbed-8998c13dcbe2")
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_"
        }
    },
    {
        "name" : "system.views",
        "type" : "collection",
        "options" : {
            
        },
        "info" : {
            "readOnly" : false,
            "uuid" : UUID("3f458338-0678-4d0f-a0cf-eacbd43c8cad")
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_"
        }
    }
]

The definition of db.getCollectionInfos() actually goes like this:

db.getCollectionInfos(filter, nameOnly, authorizedCollections)

So we can use the filter parameter to filter the list of collections based on a query expression. This can be applied against any field returned by the method.

You can also use the nameOnly parameter to specify that the method should return only the names of the collections and views.

The authorizedCollections parameter, when set to true and used with nameOnly: true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. In this case, the command returns only those collections for which the user has privileges.

Example of using db.getCollectionInfos() with these parameters:

db.getCollectionInfos( {}, true, true )

Result:

[
    {
        "name" : "employees",
        "type" : "collection"
    },
    {
        "name" : "pets",
        "type" : "collection"
    },
    {
        "name" : "pettypes",
        "type" : "view"
    },
    {
        "name" : "products",
        "type" : "collection"
    },
    {
        "name" : "system.views",
        "type" : "collection"
    }
]

Here’s one where I filter it to just a specific name:

db.getCollectionInfos( { name: "pets" }, true, true )

Result:

 [ { "name" : "pets", "type" : "collection" } ] 

And here’s what happens when I remove the last two arguments:

db.getCollectionInfos( { name: "pets" } )

Result:

[
    {
        "name" : "pets",
        "type" : "collection",
        "options" : {
            
        },
        "info" : {
            "readOnly" : false,
            "uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
        },
        "idIndex" : {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_"
        }
    }
]