Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

db.getCollectionInfos()

On this page

  • Definition
  • Required Access
  • Behavior
  • Example
db.getCollectionInfos(filter, nameOnly, authorizedCollections)

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. For details, see Required Access.

The db.getCollectionInfos() helper wraps the listCollections command.

The db.getCollectionInfos() method has the following optional parameter:

Parameter
Type
Description
filter
document

Optional. A query expression to filter the list of collections.

You can specify a query expression on any of the fields returned by db.getCollectionInfos().

nameOnly
boolean

Optional. A flag to indicate whether the command should return just the collection/view names and type or return both the name and other information.

Returning just the name and type (view or collection) does not take collection-level locks whereas returning full collection information locks each collection in the database.

The default value is false.

Note

When nameOnly is true, your filter expression can only filter based on a collection's name and type. No other fields are available.

New in version 4.0.

authorizedCollections
boolean

Optional. A flag, when set to true and used with nameOnly: true, that allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced.

When both authorizedCollections and nameOnly options are set to true, the command returns only those collections for which the user has privileges. For example, if a user has find action on specific collections, the command returns only those collections; or, if a user has find or any other action, on the database resource, the command lists all collections in the database.

The default value is false. That is, the user must have listCollections action on the database to run the command.

For a user who has listCollections action on the database, this option has no effect since the user has privileges to list the collections in the database.

When used without nameOnly: true, this option has no effect. That is, the user must have the required privileges to run the command when access control is enforced. Otherwise, the user is unauthorized to run the command.

New in version 4.0.

Changed in version 3.2.

MongoDB 3.2 added support for document validation. db.getCollectionInfos() includes document validation information in the options document.

db.getCollectionInfos() does not return validationLevel and validationAction unless they are explicitly set.

Since db.getCollectionInfos() is a wrapper around the listCollections, users must have the same privileges as listCollections when access control is enforced.

To run listCollections when access control is enforced, users must, in general, have privileges that grant listCollections action on the database. For example, the following privilege grants users to run db.getCollectionInfos() against the test database:

{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }

The built-in role read provides the privilege to run listCollections for a specific database.

Starting in version 4.0, however, user without the required privilege can run the command with both authorizedCollections and nameOnly options set to true. In this case, the command returns just the name and type of the collection(s) to which the user has privileges.

For example, consider a user with a role that grants just the following privilege:

{ resource: { db: "test", collection: "foo" }, actions: [ "find" ] }

The user can run the command if the command includes both authorizedCollections and nameOnly options set to true (with or without the filter option):

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

The operation returns the name and type of the foo collection.

However, the following operations (with or without the filter option) error for the user without the required access:

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

Starting in version 4.0 of the mongo shell, show collections is equivalent to:

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
  • 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.

When a version 4.0 mongo shell is connected to an earlier version MongoDB deployment that does not support authorizedCollections and nameOnly options,

Starting in MongoDB 4.2, if the client that issued db.getCollectionInfos() disconnects before the operation completes, MongoDB marks db.getCollectionInfos() for termination using killOp.

Starting in MongoDB 4.4, to run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.

In previous versions, the operations also run when the member is in STARTUP2. The operations wait until the member transitioned to RECOVERING.

The following returns information for all collections in the example database:

use example
db.getCollectionInfos()

The method returns an array of documents that contain collection information:

[
{
"name" : "employees",
"type" : "collection",
"options" : {
"flags" : 1,
"validator" : {
"$or" : [
{
"phone" : {
"$exists" : true
}
},
{
"email" : {
"$exists" : true
}
}
]
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.employees"
}
},
{
"name" : "products",
"type" : "collection",
"options" : {
"flags" : 1
},
"info" : {
"readOnly" : false,
"uuid" : UUID("1bc898b2-3b91-45e4-9d8b-0be462d5a157")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.products"
}
},
{
"name" : "mylogs",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 256
},
"info" : {
"readOnly" : true,
"uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.mylogs"
}
}
]

To request collection information for a specific collection, specify the collection name when calling the method, as in the following:

use example
db.getCollectionInfos( { name: "employees" } )

The method returns an array with a single document that details the collection information for the employees collection in the example database.

[
{
"name" : "employees",
"type" : "collection",
"options" : {
"flags" : 1,
"validator" : {
"$or" : [
{
"phone" : {
"$exists" : true
}
},
{
"email" : {
"$exists" : true
}
}
]
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.employees"
}
}
]

You can specify a filter on any of the fields returned by db.getCollectionInfos().

For example, the following command returns information for all collections in the example database where info.readOnly is true:

use example
db.getCollectionInfos( { "info.readOnly" : true } )

The command returns the following:

[
{
"name" : "mylogs",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 256
},
"info" : {
"readOnly" : true,
"uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.mylogs"
}
}
]
←  db.getCollection()db.getCollectionNames() →