On this page本页内容
listCollections¶Retrieve information, i.e. the name and options, about the collections and views in a database. Specifically, the command returns a document that contains information with which to create a cursor to the collection information. The mongo shell provides the db.getCollectionInfos() and the db.getCollectionNames() helper methods.
The command has the following form:
The listCollections command can take the following optional field:
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
|
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
Use a filter to limit the results of listCollections. You can specify a filter on any of the fields returned in the listCollections result set.
Changed in version 4.0.在版本4.0中更改。
The listCollection command takes Intent Shared lock on the database. In previous versions, the command takes Shared lock on the database.
Unless the nameOnly option is specified, the command also takes an Intent Shared lock on each of the collections in turn while holding the Intent Shared lock on the database.
Starting in MongoDB 4.2, if the client that issued the listCollections disconnects before the operation completes, MongoDB marks the listCollections 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.
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.listCollections.cursor¶A document that contains information with which to create a cursor to documents that contain collection names and options. The cursor information includes the cursor id, the full namespace for the command, as well as the first batch of results. Each document in the batch output contains the following fields:
| name | String | Name of the collection. |
| type | String | Type of data store. Returns collection for collections and view for views. |
| options | Document | Collection options. These options correspond directly to the options available in |
| info | Document | Lists the following fields related to the collection:
|
| idIndex | Document | Provides information on the _id index for the collection. |
listCollections.ok¶The return value for the command. A value of 1 indicates success.
The following example uses the db.getCollectionInfos() helper to return information for all collections in the records database:
See also参阅