定义
db.getCollectionInfos(filter, nameOnly, authorizedCollections)返回包含当前数据库的集合或 视图信息(例如名称和选项)的文档大量。结果取决于用户的权限。 有关详细信息,请参阅所需的访问权限。
db.getCollectionInfos()助手会封装listCollections命令。db.getCollectionInfos()方法具有以下可选参数:Parameter类型说明filter文档
可选。用于筛选集合列表的查询表达式。
nameOnly布尔
可选。 指示命令是应仅返回名称和类型(
view、collection或timeseries)还是同时返回名称和其他信息的标志。默认值为
false。当
nameOnly为true时,filter表达式只能根据集合的名称和类型进行筛选。没有其他可用字段。authorizedCollections布尔
可选。一个标记,当设置为
true并与nameOnly: true一起使用时,允许没有所要求的特权(即数据库上的listCollections操作)的用户在强制执行访问控制期间运行命令。当
authorizedCollections和nameOnly选项均设置为 true 时,该命令仅返回用户对其拥有特权的集合。例如,如果用户有权对特定集合执行find操作,则该命令仅返回这些集合;或者,如果用户有权对数据库资源执行find或任何其他操作,则该命令将列出数据库中的所有集合。默认值为
false。也就是说,用户必须有权对数据库执行listCollections操作才能运行命令。对于有权对数据库执行
listCollections操作的用户,此选项无效,因为该用户有权列出数据库中的集合。不与
nameOnly: true一起使用时,此选项无效。也就是说,在实施访问控制期间,用户必须具有运行命令所需的特权。否则,用户无权运行命令。
兼容性
此方法可用于以下环境中托管的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
必需的访问权限
由于 db.getCollectionInfos() 是 listCollections 的包装器,因此在强制执行访问控制时,用户必须拥有与 listCollections 相同的权限。
强制执行访问控制时,listCollections 命令要求执行 listCollections 操作。用户必须具有授予对数据库执行 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仅列出用户对其拥有特权的集合。
行为
客户端断开连接
从 MongoDB 4.2 开始,如果在操作完成之前,发出 db.getCollectionInfos() 的客户端断开连接,MongoDB 将使用killOp 将 db.getCollectionInfos() 标记为终止。
副本集节点状态限制
要在副本集成员上运行,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" } } ]
要请求特定 集合的集合信息,请在调用该方法时指定集合名称,如下所示:
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.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" } } ]