定义
db.getCollectionInfos(filter, options)返回包含当前数据库的集合或 视图信息(例如名称和选项)的文档大量。结果取决于用户的权限。有关详细信息,请参阅 所需访问权限。
db.getCollectionInfos()listCollections辅助程序会封装 命令。
语法
db.getCollectionInfos() 方法具有以下参数:
Parameter | 类型 | 说明 |
|---|---|---|
| 文档 | |
| 文档 |
选项
您可以在 db.getCollectionInfos() 命令的 options文档中指定以下选项:
选项 | 类型 | 说明 |
|---|---|---|
| 布尔 | 可选。指示命令是仅返回名称和类型( 默认值为 当 |
| 布尔 |
当
默认值为 对于有权对数据库执行 在没有 |
兼容性
此方法可用于以下环境中托管的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
必需的访问权限
listCollections实施访问权限控制时,listCollections 命令及其包装器db.getCollectionInfos() 需要 动作。用户必须具有授予对数据库执行listCollections 动作的特权才能运行listCollections 。
例如,以下命令授予对 test 数据库运行 db.getCollectionInfos() 的特权:
{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
内置角色 read 提供为特定数据库运行 listCollections 的特权。
当 authorizedCollections 和 nameOnly 都设置为 true 时,没有所需的 read 特权的用户可以运行 listCollections。在这种情况下,该命令将返回用户对其拥有特权的集合的名称和类型。
例如,假设某个用户具有授予以下 find 特权的角色:
{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }
如果 authorizedCollections 和 nameOnly 都设置为 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 } )
show collections
mongosh 方法 show collections 类似于:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
对于具有所需访问权限的用户,
show collections将列出数据库的非系统集合。对于不具有所需访问权限的用户,
show collections仅列出用户对其拥有特权的集合。
行为
客户端断开连接
如果发出 db.getCollectionInfos() 的客户端在操作完成之前断开连接,MongoDB 会使用 db.getCollectionInfos() 将 killOp 标记为终止。
副本集节点状态限制
要在副本集成员上运行,listCollections 操作需要成员处于 PRIMARY 或 SECONDARY 状态。如果节点处于其他状态,如 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.readOnly 为 true:
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" } } ]