Docs HomeMongoDB Manual

db.getCollectionInfos()

Definition定义

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()方法具有以下可选参数:

Parameter参数Type类型Description描述
filterdocumentOptional.可选的。A query expression to filter the list of collections.用于筛选集合列表的查询表达式。
You can specify a query expression on any of the fields returned by db.getCollectionInfos(). 可以对db.getCollectionInfos()返回的任何字段指定查询表达式。
nameOnlybooleanOptional.可选的。A flag to indicate whether the command should return just the collection/view names and type or return both the name and other information.一个标志,用于指示命令应仅返回集合/视图名称和类型,还是同时返回名称和其他信息。
Returning just the name and type (view or collection) does not take collection-level locks whereas returning full collection information locks each collection in the database.仅返回名称和类型(viewcollection)不会获得集合级别的锁定,而返回完整的集合信息会锁定数据库中的每个集合。
The default value is false. 默认值为false
Note
When nameOnly is true, your filter expression can only filter based on a collection's name and type. nameOnlytrue时,filter表达式只能根据集合的名称和类型进行筛选。No other fields are available. 没有其他字段可用。
authorizedCollectionsbooleanOptional.可选的。A flag, when set to true and used with nameOnly: true, that allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced.一个标志,当设置为true并与nameOnly:true一起使用时,它允许没有所需权限(即数据库上的listCollections操作)的用户在强制执行访问控制时运行该命令。
When both authorizedCollections and nameOnly options are set to true, the command returns only those collections for which the user has privileges. authorizedCollectionsnameOnly选项都设置为true时,该命令仅返回用户具有权限的集合。For example, if a user has find action on specific collections, the command returns only those collections; or, if a user has find or any other action, on the database resource, the command lists all collections in the database.例如,如果用户对特定集合执行了find操作,则该命令仅返回这些集合;或者,如果用户对数据库资源进行了查找或任何其他操作,则该命令会列出数据库中的所有集合。
The default value is false. 默认值为falseThat is, the user must have listCollections action on the database to run the command.也就是说,用户必须对数据库执行listCollections操作才能运行该命令。
For a user who has listCollections action on the database, this option has no effect since the user has privileges to list the collections in the database.对于对数据库具有listCollections操作的用户,此选项无效,因为该用户具有在数据库中列出集合的权限。
When used without nameOnly: true, this option has no effect. 在不带nameOnly:true的情况下使用时,此选项无效。That is, the user must have the required privileges to run the command when access control is enforced. Otherwise, the user is unauthorized to run the command. 也就是说,在实施访问控制时,用户必须具有运行命令所需的权限。否则,用户无权运行该命令。

Required Access所需访问权限

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相同的权限。

The listCollections command requires the listCollections action when access control is enforced. Users must have privileges that grant the listCollections action on the database to run listCollections.在实施访问控制时,listCollections命令需要listCollections操作。用户必须具有对数据库授予listCollections操作的权限才能运行listCollections

For example, the following command grants the privilege 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的权限。

Users without the required read privilege can run listCollections when authorizedCollections and nameOnly are both set to true. In this case, the command returns the names and types for collection(s) where the user has privileges.authorizedCollectionsnameOnly都设置为true时,没有所需read权限的用户可以运行listCollections。在这种情况下,该命令返回用户具有权限的集合的名称和类型。

For example, consider a user with a role that grants the following find privilege:例如,考虑具有授予以下find权限的角色的用户:

{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }

The user can run listCollections if authorizedCollections and nameOnly are both set to true.如果authorizedCollectionsnameOnly都设置为true,则用户可以运行listCollections

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)

The operation returns the name and type of the currentQuarter collection.该操作返回currentQuarter集合的名称和类型。

However, the following operations return an error if the user does not have the required access authorization:但是,如果用户没有所需的访问权限,则以下操作将返回错误:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true
}
)

db.runCommand(
{
listCollections: 1.0,
nameOnly: true
}
)

show collections

The mongosh method show collections is similar to:mongosh方法show collections类似于:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)
  • For users with the required access, show collections lists the non-system collections for the database.对于具有所需访问权限的用户,show collections会列出数据库的非系统集合。
  • For users without the required access, show collections lists only the collections for which the users has privileges.对于没有所需访问权限的用户,show collections仅列出用户具有权限的集合。

Behavior行为

Client Disconnection客户端断开连接

Starting in MongoDB 4.2, if the client that issued db.getCollectionInfos() disconnects before the operation completes, MongoDB marks db.getCollectionInfos() for termination using killOp.从MongoDB 4.2开始,如果发出db.getCollectionInfos()的客户端在操作完成前断开连接,MongoDB会使用killOp标记db.getCollectionInfos()终止。

Replica Set Member State Restriction副本集成员状态限制

Starting in MongoDB 4.4, to run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. 从MongoDB 4.4开始,要在副本集成员上运行,listCollections操作需要该成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于其他状态,例如STARTUP2,则操作将出错。

In previous versions, the operations also run when the member is in STARTUP2. 在以前的版本中,成员处于STARTUP2时也会运行这些操作。The operations wait until the member transitioned to RECOVERING.操作将等待,直到成员转换到RECOVERING

Example实例

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.readOnlytrue的所有集合的信息:

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"
}
}
]