Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

db.getCollectionInfos()

在此页面上

  • 定义
  • 必需的访问权限
  • 行为
  • 例子
db.getCollectionInfos(filter, nameOnly, authorizedCollections)

返回包含当前数据库的集合或视图信息(例如名称和选项)的文档数组。结果取决于用户的权限。有关详细信息,请参阅所需访问权限。

db.getCollectionInfos() 助手会封装 listCollections 命令。

db.getCollectionInfos()方法具有以下可选参数:

参数
类型
说明
filter
文档

可选。用于筛选集合列表的查询表达式。

您可以在 返回的任何字段 db.getCollectionInfos()上指定查询表达式。

nameOnly
布尔

可选。指示命令是应仅返回名称和类型( viewcollectiontimeseries )还是同时返回名称和其他信息的标志。

默认值为 false

注意

nameOnlytrue 时,filter 表达式只能根据集合的名称和类型进行筛选。没有其他可用字段。

authorizedCollections
布尔

可选。一个标记,当设置为 true 并与 nameOnly: true 一起使用时,允许没有所要求的特权(即数据库上的listCollections 操作)的用户在强制执行访问控制期间运行命令。

authorizedCollectionsnameOnly 选项均设置为 true 时,该命令仅返回用户对其拥有特权的集合。例如,如果用户有权对特定集合执行 find 操作,则该命令仅返回这些集合;或者,如果用户有权对数据库资源执行 find 或任何其他操作,则该命令将列出数据库中的所有集合。

默认值为 false。也就是说,用户必须有权对数据库执行 listCollections 操作才能运行命令。

对于有权对数据库执行 listCollections 操作的用户,此选项无效,因为该用户有权列出数据库中的集合。

不与 nameOnly: true 一起使用时,此选项无效。也就是说,在实施访问控制期间,用户必须具有运行命令所需的特权。否则,用户无权运行命令。

由于 db.getCollectionInfos()listCollections 的包装器,因此在强制执行访问控制时,用户必须拥有与 listCollections 相同的权限。

强制执行访问控制时,listCollections 命令要求执行 listCollections 操作。用户必须具有授予对数据库执行 listCollections 操作的权限才能运行 listCollections

例如,以下命令授予对 test 数据库运行 db.getCollectionInfos() 的特权:

{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }

内置角色 read 提供为特定数据库运行 listCollections 的特权。

authorizedCollectionsnameOnly 都设置为 true 时,没有所需的 read 特权的用户可以运行 listCollections。在这种情况下,该命令将返回用户对其拥有特权的集合的名称和类型。

例如,假设某个用户具有授予以下 find 特权的角色:

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

如果 authorizedCollectionsnameOnly 都设置为 true,则用户可以运行listCollections

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

该操作将返回 currentQuarter 集合的名称和类型。

但是,如果用户没有所需访问授权,以下操作会返回错误:

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

mongosh 方法 show collections 类似于:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)
  • 对于具有所需访问权限的用户,show collections 将列出数据库的非系统集合。

  • 对于不具有所需访问权限的用户,show collections 仅列出用户对其拥有特权的集合。

从 MongoDB 4.2 开始,如果在操作完成之前,发出 db.getCollectionInfos() 的客户端断开连接,MongoDB 将使用killOpdb.getCollectionInfos() 标记为终止。

要在副本集成员上运行,listCollections 操作需要成员处于 PRIMARYSECONDARY 状态。如果节点处于其他状态,如 STARTUP2,则操作错误。

以下返回 example 数据库中所有集合的信息:

use example
db.getCollectionInfos()

该方法返回包含集合信息的文档数组:

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

要请求特定 集合的集合信息,请在调用该方法时指定集合名称,如下所示:

use example
db.getCollectionInfos( { name: "employees" } )

该方法返回一个包含单个文档的数组,该文档详细说明了 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"
}
}
]

您可以对db.getCollectionInfos()返回的任何字段指定筛选器。

例如,以下命令返回 example 数据库中所有集合的信息,其中,info.readOnlytrue

use example
db.getCollectionInfos( { "info.readOnly" : true } )

该命令返回以下内容:

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

后退

db.getCollection

来年

db.getCollectionNames