定义
db.getCollectionInfos(filter, options)返回一个包含当前数据库中集合或视图信息(例如名称和选项)的文档数组。结果取决于用户的权限。有关详细信息,请参阅所需的访问权限。
db.getCollectionInfos()辅助程序会封装listCollections命令。
语法
db.getCollectionInfos() 方法具有以下参数:
Parameter | 类型 | 说明 |
|---|---|---|
| 文档 | 可选。用于过滤集合列表的查询谓词。 您可以在 |
| 文档 | Optional. A document that specifies additional options for the command. For a list of available options, see Options. |
选项
您可以在 db.getCollectionInfos() 命令的 options文档中指定以下选项:
选项 | 类型 | 说明 |
|---|---|---|
| 布尔 | 可选。指示命令是仅返回名称和类型( 默认值为 当 For an example, see Return Only Collection Names. |
| 布尔 | 可选。当设立为 当
默认值为 对于有权对数据库执行 不与 |
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
必需的访问权限
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 的特权。
当 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仅列出用户对其拥有特权的集合。
行为
客户端断开连接
If the client that issued db.getCollectionInfos() disconnects before the operation completes, MongoDB marks db.getCollectionInfos() for termination using 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" } } ]