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. 具体来说,该命令返回一个文档,该文档包含用于创建指向集合信息的游标的信息。mongosh
provides the 提供了db.getCollectionInfos()
and the db.getCollectionNames()
helper methods.db.getCollectionInfos()
和db.getCollectionNames()
帮助器方法。
The command has the following form:命令的格式如下:
{ listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> }
The listCollections
command can take the following optional field:listCollections
命令可以采用以下可选字段:
filter | document |
|
nameOnly | boolean |
|
authorizedCollections | boolean |
|
comment | any |
|
Use a filter to limit the results of 使用筛选器限制listCollections
. listCollections
的结果。You can specify a 您可以对filter
on any of the fields returned in the listCollections
result set.listCollections
结果集中返回的任何字段指定filter
。
Changed in version 4.0.在版本4.0中更改。
The listCollections
command takes Intent Shared lock on the database. listCollections
命令获取数据库的Intent Shared锁。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.nameOnly
选项,否则该命令还会依次对每个集合执行Intent Shared锁,同时对数据库执行Intent Share锁。
Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果在操作完成之前发出listCollections
disconnects before the operation completes, MongoDB marks the listCollections
for termination (i.e. killOp
on the operation).listCollections
的客户端断开连接,MongoDB会标记listCollections
终止(即操作上的killOp
)。
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,listCollections
operations require the member to be in PRIMARY
or SECONDARY
state. listCollections
操作要求成员处于PRIMARY
或SECONDARY
状态。If the member is in another state, such as 如果成员处于其他状态,如STARTUP2
, the operation errors.STARTUP2
,则操作将出错。
In previous versions, the operations can also be run when the member is in 在以前的版本中,也可以在成员处于STARTUP2
. STARTUP2
时运行操作。However, the operations wait until the member transitions to 但是,操作将等待成员转换到RECOVERING
.RECOVERING
。
To run 若要在强制执行访问控制时运行listCollections
when access control is enforced, users must, in general, have privileges that grant listCollections
action on the database. listCollections
,用户通常必须具有在数据库上授予listCollections
操作的权限。For example, the following privilege grants users to run 例如,以下权限授予用户对db.getCollectionInfos()
against the test
database:test
数据库运行db.getCollectionInfos()
:
{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
The built-in role 内置角色read
provides the privilege to run listCollections
for a specific database.read
提供了为特定数据库运行listCollections
的权限。
Starting in version 4.0, however, user without the required privilege can run the command with both 但是,从4.0版开始,没有所需权限的用户可以在authorizedCollections
and nameOnly
options set to true
. authorizedCollection
和nameOnly
选项都设置为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):authorizedCollection
和nameOnly
选项,则用户可以运行该命令(带有或不带有筛选器选项):
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
The operation returns the name and type of the 该操作返回foo
collection.foo
集合的名称和类型。
However, the following operations (with or without the 但是,对于没有所需访问权限的用户,以下操作(使用或不使用filter
option) error for the user without the required access:filter
选项)会出错:
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, show collections
is equivalent to:mongo
shell的4.0版本开始,show collections
相当于:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
show collections
lists the non-system collections for the database.show collections
列出了数据库的非系统集合。show collections
lists only the collections for which the users has privileges.show collections
仅列出用户具有权限的集合。When a version 4.0 当4.0版mongo
shell is connected to an earlier version MongoDB deployment that does not support authorizedCollections
and nameOnly
options,mongo
shell连接到不支持authorizedCollection
和nameOnly
选项的早期版本MongoDB部署时,
listCollections
.listCollections
所需的访问权限。show collections
, MongoDB uses the authenticatedUserPrivileges
field returned by connectionStatus
to return an approximate list of collections for the user.show collections
,MongoDB将使用connectionStatus
返回的authenticatedUserPrivileges
字段为用户返回集合的大致列表。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. 游标信息包括游标id、命令的完整名称空间以及第一批结果。Each document in the batch output contains the following fields:批处理输出中的每个文档都包含以下字段:
name | String | |
type | String | collection for collections, view for views, and timeseries for time series collection.collection 、视图的view 和时间序列集合的timeseries 。
|
options | Document |
|
info | Document |
|
idIndex | Document | _id index for the collection._id 索引的信息。
|
The following example uses the 以下示例使用db.getCollectionInfos()
helper to return information for all collections in the records
database:db.getCollectionInfos()
帮助器返回records
数据库中所有集合的信息:
use records db.getCollectionInfos();