listCollections
定义
listCollections
检索数据库中集合和视图的信息,包括名称和创建选项。视图
listCollections
命令返回一个文档,其中包含数据库中所有集合和视图的无序列表。您可以使用返回的文档在集合上创建一个游标。mongosh
提供了db.getCollectionInfos()
和db.getCollectionNames()
辅助方法。
兼容性
此命令可在以下环境中部署
MongoDB Atlas:云中 MongoDB 部署的完全托管服务
注意
此命令在所有 MongoDB Atlas 集群中受支持。有关 Atlas 对所有命令的支持信息,请参阅 不受支持的命令。
MongoDB Enterprise:基于订阅的、自我管理的 MongoDB 版本
MongoDB Community:源代码可用的、免费使用且自我管理的 MongoDB 版本
语法
该命令具有以下语法
db.runCommand( { listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> } )
命令字段
该命令可以接受以下可选字段
字段 | 类型 | 描述 |
---|---|---|
筛选器 | 文档 | 可选。一个查询谓词,用于筛选集合列表。 您可以在返回的字段上指定查询谓词,这些字段由 |
nameOnly | 布尔值 | 可选。一个标志,用于指示命令是否仅返回名称和类型( 默认值是 当 |
authorizedCollections | 布尔值 | 可选。一个标志,当设置为 当同时设置 默认值是 对于具有数据库上 当不与 |
comment | 任何 | 可选。用户提供的注释,用于附加到此命令。一旦设置,此注释将出现在以下位置的此命令记录旁边
注释可以是任何有效的 BSON 类型(字符串、整数、对象、数组等)。 在 |
行为
过滤器
使用过滤器限制 listCollections
的结果。您可以在 listCollections
结果集中返回的任何 字段 上指定 filter
。
锁
listCollections
锁的行为
在 MongoDB 5.0 之前,
listCollections
在listCollections
持有数据库的意向共享锁时,会对数据库中的每个集合都加一个意向共享锁。从 MongoDB 5.0 开始,
listCollections
不会对集合或数据库加意向共享锁。listCollections
不会被持有集合独占写锁的操作阻塞。
关于锁的更多信息,请参阅FAQ:并发。
客户端断开连接
从 MongoDB 4.2 开始,如果发出 listCollections
的客户端在操作完成之前断开连接,MongoDB 将使用 killOp
将 listCollections
标记为终止。
副本集成员状态限制
要在副本集成员上运行,listCollections
操作需要该成员处于 PRIMARY
或 SECONDARY
状态。如果成员处于其他状态,例如 STARTUP2
,操作将报错。
所需访问权限
当启用访问控制时,listCollections
命令需要listCollections
操作。用户必须拥有在数据库上执行listCollections
操作的权限。
例如,以下命令为test
数据库授予运行db.getCollectionInfos()
的权限
{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
内置角色read
为特定数据库提供运行listCollections
的权限。
如果没有所需的read
权限,用户可以在authorizedCollections
和nameOnly
都设置为true
时运行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
仅列出用户有权限的集合。
输出
listCollections.cursor
包含用于创建包含集合名称和选项的文档游标的文档。游标信息包括游标ID、命令的完整命名空间以及第一批结果。批输出中的每个文档都包含以下字段
字段类型描述名称字符串集合名称。类型字符串选项文档集合选项。
这些选项直接对应于
db.createCollection()
中可用的选项。有关选项的描述,请参阅db.createCollection()
。信息文档列出与集合相关的以下字段
- 只读
布尔值
。如果为true
,则数据存储为只读。- uuid
- UUID。一旦建立,集合UUID不会更改。集合UUID在副本集成员和分片集群中的分片中保持相同。
idIndex文档提供有关集合的_id
索引的信息。
示例
列出所有集合
数据库 music
包含三个集合,分别是 motorhead
、taylorSwift
和 ramones
。
要列出数据库中的集合,可以使用内置的 mongosh
命令,show collections。
show collections
输出结果
motorhead ramones taylorSwift
要使用 listCollections
集合命令获取类似的列表,使用 nameOnly
选项。
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 }
了解更多
关于集合选项
关于集合信息