db.getCollectionInfos()
Definition
New in version 3.0.
Returns an array of documents with collection or view information, such as name and options, forthe current database. The results depend on the user’sprivilege. For details, see Required Access.
The db.getCollectionInfos()
helper wraps thelistCollections
command.
The db.getCollectionInfos()
method has the followingoptional parameter:
ParameterTypeDescriptionfilter
documentOptional. A query expression to filter the list of collections.
You can specify a query expression on any of the fieldsreturned by db.getCollectionInfos
.nameOnly
booleanOptional. A flag to indicate whether the command should return just thecollection/view names and type or return both the name and other information.
Returning just the name and type (view
or collection
) doesnot take collection-level locks whereas returning full collectioninformation locks each collection in the database.
The default value is false
.
Note
When nameOnly
is true
, your filter
expression can onlyfilter based on a collection’s name and type. No other fields areavailable.
New in version 4.0.
authorizedCollections
booleanOptional. A flag, when set to true
and used with nameOnly: true
, thatallows a user without the required privilege (i.e.listCollections
action on the database) to run thecommand when access control is enforced.
When both authorizedCollections
and nameOnly
options are setto true, the command returns only those collections for which the userhas privileges. For example, if a user has find
actionon specific collections, the command returns only those collections; or,if a user has find
or any other action, on thedatabase resource, the command lists all collections in the database.
The default value is false
. That is, the user must havelistCollections
action on the database to run thecommand.
For a user who has listCollections
action on thedatabase, this option has no effect since the user has privileges tolist 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 thecommand when access control is enforced. Otherwise, the user isunauthorized 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.
Required Access
Since db.getCollectionInfos()
is a wrapper around thelistCollections
, users must have the same privileges aslistCollections
when access control is enforced.
To run listCollections
when access control is enforced,users must, in general, have privileges that grantlistCollections
action on the database. For example,the following privilege grants users to rundb.getCollectionInfos()
against the test
database:
- { resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
The built-in role read
provides the privilege to runlistCollection
for a specific database.
Starting in version 4.0, however, user without the required privilegecan run the command with both authorizedCollections
andnameOnly
options set to true
. In this case, the command returnsjust the name and type of the collection(s) to which the user hasprivileges.
For example, consider a user with a role that grants just the followingprivilege:
- { resource: { db: "test", collection: "foo" }, actions: [ "find" ] }
The user can run the command if the command includes bothauthorizedCollections
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 } )
show collections
Starting in version 4.0 of the mongo
shell, showcollections
is equivalent to:
- db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
- For users with the required access,
show collections
lists thenon-system collections for the database. - For users without the required access,
show collections
listsonly the collections for which the users has privileges.
Earlier MongoDB Versions
When a version 4.0 mongo
shell is connected to anearlier version MongoDB deployment that does not supportauthorizedCollections
and nameOnly
options,
- A user must have the required access to run
listCollection
. - If a user does not have required access and runs
showcollections
, MongoDB uses theauthenticatedUserPrivileges
fieldreturned byconnectionStatus
to return an approximatelist of collections for the user.
Behavior
Client Disconnection
Starting in MongoDB 4.2, if the client that issued the db.getCollectionInfos()
disconnects before the operation completes, MongoDB marksthe db.getCollectionInfos()
for termination (i.e. killOp
on theoperation).
Example
The following returns information for all collections in theexample
database:
- use example
- db.getCollectionInfos()
The method returns an array of documents that contain collectioninformation:
- [
- {
- "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 thecollection information for the employees
collection in theexample
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 bygetCollectionInfos
.
For example, the following command returns information for allcollections in the example
database where info.readOnly
istrue
:
- 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"
- }
- }
- ]