On this page本页内容
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:
filter |
document |
You can specify a query expression on any of the fields returned by |
nameOnly |
boolean |
Returning just the name and type ( The default value is Note When
|
authorizedCollections |
boolean |
When both The default value is For a user who has When used without
|
Changed in version 3.2.在版本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:
The built-in role read
provides the privilege to run listCollection
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:
The user can run the command if the command includes both
authorizedCollections
and nameOnly
options set to true
(with or without the filter
option):
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:
show collections
¶Starting in version 4.0 of the mongo
shell, show collections
is equivalent to:
show collections
lists the non-system collections for the database.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,
listCollection
.show collections
, MongoDB uses the authenticatedUserPrivileges
field returned by connectionStatus
to return an approximate list of collections for the user.Starting in MongoDB 4.2, if the client that issued the db.getCollectionInfos()
disconnects before the operation completes, MongoDB marks the db.getCollectionInfos()
for termination (i.e. killOp
on the operation).
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 can also be run when the member is in STARTUP2
. However, the operations wait until the member transitions to RECOVERING
.
The following returns information for all collections in the example
database:
The method returns an array of documents that contain collection information:
To request collection information for a specific collection, specify the collection name when calling the method, as in the following:
The method returns an array with a single document that details the collection information for the employees
collection in the example
database.
You can specify a filter on any of the fields returned by getCollectionInfos
.
For example, the following command returns information for all collections in the example
database where info.readOnly
is true
:
The command returns the following: