db.getCollectionInfos()
On this page本页内容
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.有关详细信息,请参阅所需访问权限。Thedb.getCollectionInfos()
helper wraps thelistCollections
command.db.getCollectionInfos()
帮助程序包装listCollections
命令。Thedb.getCollectionInfos()
method has the following optional parameter:db.getCollectionInfos()
方法具有以下可选参数:Parameter参数Type类型Description描述filter
document Optional.可选的。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()
返回的任何字段指定查询表达式。nameOnly
boolean Optional.可选的。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
orcollection
) does not take collection-level locks whereas returning full collection information locks each collection in the database.view
或collection
)不会获得集合级别的锁定,而返回完整的集合信息会锁定数据库中的每个集合。
The default value is默认值为false
.false
。NoteWhen当nameOnly
istrue
, yourfilter
expression can only filter based on a collection's name and type.nameOnly
为true
时,filter
表达式只能根据集合的名称和类型进行筛选。No other fields are available.没有其他字段可用。authorizedCollections
boolean Optional.可选的。A flag, when set to一个标志,当设置为true
and used withnameOnly: 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
andnameOnly
options are set to true, the command returns only those collections for which the user has privileges.authorizedCollections
和nameOnly
选项都设置为true
时,该命令仅返回用户具有权限的集合。For example, if a user has例如,如果用户对特定集合执行了find
action on specific collections, the command returns only those collections; or, if a user hasfind
or any other action, on the database resource, the command lists all collections in the database.find
操作,则该命令仅返回这些集合;或者,如果用户对数据库资源进行了查找或任何其他操作,则该命令会列出数据库中的所有集合。
The default value is默认值为false
.false
。That 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.authorizedCollections
和nameOnly
都设置为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
.authorizedCollections
和nameOnly
都设置为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 从MongoDB 4.2开始,如果发出db.getCollectionInfos()
disconnects before the operation completes, MongoDB marks db.getCollectionInfos()
for termination using killOp
.db.getCollectionInfos()
的客户端在操作完成前断开连接,MongoDB会使用killOp
标记db.getCollectionInfos()
终止。
Replica Set Member State Restriction副本集成员状态限制
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 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.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"
}
}
]