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.db.getCollectionInfos()
帮助器包装listCollections
命令。
The db.getCollectionInfos()
method has the following optional parameter:db.getCollectionInfos()
方法具有以下可选参数:
filter | document |
|
nameOnly | boolean |
The default value is
|
authorizedCollections | boolean |
|
Changed in version 3.2.在版本3.2中更改。
MongoDB 3.2 added support for document validation. MongoDB 3.2增加了对文档验证的支持。db.getCollectionInfos()
includes document validation information in the 在options
document.options
文档中包括文档验证信息。
db.getCollectionInfos()
does not return 除非显式设置了validationLevel
and validationAction
unless they are explicitly set.validationLevel
和validationAction
,否则不会返回它们。
Since 由于db.getCollectionInfos()
is a wrapper around the listCollections
, users must have the same privileges as listCollections
when access control is enforced.db.getCollectionInfos()
是listCollections
的包装器,因此在执行访问控制时,用户必须具有与listCollections
相同的权限。
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
. authorizedCollections
和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):authorizedCollections
和nameOnly
选项,并将其设置为true
(带或不带筛选器选项),则用户可以运行该命令:
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 } )
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连接到不支持authorizedCollections
和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
字段为用户返回集合的大致列表。Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.getCollectionInfos()
disconnects before the operation completes, MongoDB marks the db.getCollectionInfos()
for termination (i.e. killOp
on the operation).db.getCollectionInfos()
的客户端在操作完成之前断开连接,MongoDB将db.getCollectionInfos()
标记为终止(即操作上的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
。
The following returns information for all collections in the 下面将返回example
database:example
数据库中所有集合的信息:
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.example
数据库中employees
集合的集合信息。
[ { "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()
.db.getCollectionInfos()
返回的任何字段上指定筛选器。
For example, the following command returns information for all collections in the 例如,以下命令返回example
database where info.readOnly
is true
:example
数据库中info.readOnly
为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" } } ]