对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db.getCollectionInfos()(mongosh方法)

db.getCollectionInfos(filter, options)

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

db.getCollectionInfos() 辅助程序会封装 listCollections 命令。

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

Parameter
类型
说明

filter

文档

可选。用于过滤集合列表的查询谓词。

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

options

文档

Optional. A document that specifies additional options for the command. For a list of available options, see Options.

您可以在 db.getCollectionInfos() 命令的 options文档中指定以下选项:

选项
类型
说明

nameOnly

布尔

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

默认值为 false

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

For an example, see Return Only Collection Names.

authorizedCollections

布尔

可选。当设立为 true 并与 nameOnly: true 一起使用时,允许没有所需权限(例如对数据库执行 listCollections 操作的用户)在实施访问权限控制时运行db.getCollectionInfos()

authorizedCollectionsnameOnly 选项均设置为 true 时,该命令仅返回用户对其拥有特权的集合。例如:

  • 如果用户对特定集合执行 find 操作,则该命令仅返回这些集合。

  • 如果用户对数据库资源具有任何操作权限,则该命令会列出数据库中的所有集合。

默认值为 false,这意味着用户必须对数据库执行 listCollections 操作才能运行命令。

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

不与 nameOnly: true 一起使用时,此选项无效。

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

The listCollections command and its wrapper db.getCollectionInfos() require the listCollections action when access control is enforced. Users must have privileges that grant the listCollections action on the database to run listCollections.

For example, the following command grants the privilege to run db.getCollectionInfos() against the test database:

{ 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 仅列出用户对其拥有特权的集合。

If the client that issued db.getCollectionInfos() disconnects before the operation completes, MongoDB marks db.getCollectionInfos() for termination using killOp.

要在副本集成员上运行,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"
}
}
]

要仅返回当前数据库中集合的名称,请指定 nameOnly: true 选项。示例:

use example
db.getCollectionInfos( {}, { nameOnly: true } )
[
{ name: 'products', type: 'collection' },
{ name: 'weather', type: 'timeseries' },
{ name: 'system.buckets.weather', type: 'collection' },
{ name: 'system.views', type: 'collection' },
{ name: 'sales', type: 'collection' }
]

要请求特定集合的集合信息,请在过滤器文档中指定集合名称。以下示例返回一个包含单个文档的数组,该文档详细说明了 example数据库中 employees集合的集合信息。

use example
db.getCollectionInfos( { name: "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"
}
}
]