定义
listCollections检索数据库中集合和视图的信息,包括名称和创建选项。
listCollections命令返回一个文档,其中包含数据库中所有集合和视图的未排序列表。您可以使用返回的文档在集合上创建游标。mongoshprovides thedb.getCollectionInfos()and thedb.getCollectionNames()helper methods, as well as the show collections command.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> } )
命令字段
该命令可以采用以下可选字段:
字段 | 类型 | 说明 |
|---|---|---|
| 文档 | 可选。用于过滤集合列表的查询谓词。 You can specify a query predicate on any of the fields returned by |
| 布尔 | 可选。指示命令是仅返回名称和类型( 默认值为 当 |
| 布尔 | 可选。一个标记,当设置为 当 默认值为 对于有权对数据库执行 不与 |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 在 |
行为
Filter
Use a filter to limit the results of listCollections. You can specify a filter on any of the fields returned in the listCollections result set.
锁
listCollections 锁行为:
MongoDB 5.0 以前的版本,当
listCollections持有数据库上的意向共享锁时,listCollections会在数据库中的每个集合上获取意向共享锁。从 MongoDB 5.0 开始,
listCollections不再对集合或数据库采用意图共享锁。listCollections不会被对集合持有独占写锁的操作阻止。
要了解锁的相关信息,请参阅常见问题解答:并发。
客户端断开连接
If the client that issued listCollections disconnects before the operation completes, MongoDB marks listCollections for termination using killOp.
副本集节点状态限制
To run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.
必需的访问权限
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.
例如,以下命令授予对 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 } )
输出
listCollections.cursor一个文档,其中包含创建游标包含集合名称和选项的文档的游标所需的信息。游标信息包括游标id、命令的完整命名空间和第一批批处理结果。批处理输出中的每个文档都包含以下字段:
字段类型说明名称
字符串
集合的名称。
类型
字符串
Type of data store. Returns
collectionfor collections,viewfor views, andtimeseriesfor time series collection.选项
文档
集合选项。
这些选项与
db.createCollection()中的选项直接对应。有关选项的说明,请参阅db.createCollection()。信息
文档
列出与集合相关的以下字段:
- 只读
boolean。如果为true,则数据存储为只读。- uuid
- UUID。建立后,集合 UUID 就不会更改。集合 UUID 在分片集群的副本集成员和分片中保持不变。
idIndex
文档
提供有关集合的
_id索引信息。
如果不需要原始命令响应,请使用 db.getCollectionInfos() 或 db.getCollectionNames() 辅助程序。
例子
列出所有集合
music 数据库包含三个集合:motorhead、taylorSwift 和 ramones。
要获取集合名称列表,运行带有 nameOnly 选项的 listCollections 命令。
db.runCommand( { listCollections: 1.0, nameOnly: true } )
输出见下:
{ cursor: { id: Long("0"), ns: 'music.$cmd.listCollections', firstBatch: [ { name: 'motorhead', type: 'collection' }, { name: 'taylorSwift', type: 'collection' }, { name: 'ramones', type: 'collection' } ] }, ok: 1 }
要获取更多详细信息,请删除 nameOnly 选项。
db.runCommand( { listCollections: 1.0 } )
输出见下:
{ cursor: { id: Long("0"), ns: 'music.$cmd.listCollections', firstBatch: [ { name: 'motorhead', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID("09ef1858-2831-47d2-a3a7-9a29a9cfeb94") }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } }, { name: 'taylorSwift', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID("6c46c8b9-4999-4213-bcef-9a36b0cff228") }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } }, { name: 'ramones', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID("7e1925ba-f2f9-4e42-90e4-8cafd434a6c4") }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } } ] }, ok: 1 }
了解详情
对于集合选项:
有关集合信息: