Docs 菜单

Docs 主页开发应用程序MongoDB Manual

listCollections

在此页面上

  • 定义
  • 行为
  • 必需的访问权限
  • 输出
  • 例子
  • 了解详情
listCollections

检索数据库中集合和视图的信息,包括名称和创建选项。

listCollections 命令返回一个文档,其中包含可用于在集合上创建游标的信息。

mongosh提供了db.getCollectionInfos()db.getCollectionNames()辅助方法。

该命令采用以下形式:

{ listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> }

listCollections命令可以采用以下可选字段:

该命令可以采用以下可选字段:

字段
类型
说明
filter
文档

可选。用于筛选集合列表的查询表达式。

您可以在 返回的任何字段 listCollections上指定查询表达式。

nameOnly
布尔

可选。该标志用于指示命令应仅返回集合/视图名称和类型,还是同时返回名称和其他信息。

仅返回名称和类型(viewcollection)不会采用集合级锁,而返回完整集合信息会锁定数据库中的每个集合。

默认值为 false

注意

nameOnlytrue 时,filter 表达式只能根据集合的名称和类型进行筛选。没有其他可用字段。

authorizedCollections
布尔

可选。一个标记,当设置为 true 并与 nameOnly: true 一起使用时,允许没有所要求的特权(即数据库上的listCollections 操作)的用户在强制执行访问控制期间运行命令。

authorizedCollectionsnameOnly 选项均设置为 true 时,该命令仅返回用户对其拥有特权的集合。例如,如果用户有权对特定集合执行 find 操作,则该命令仅返回这些集合;或者,如果用户有权对数据库资源执行 find 或任何其他操作,则该命令将列出数据库中的所有集合。

默认值为 false。也就是说,用户必须有权对数据库执行 listCollections 操作才能运行命令。

对于有权对数据库执行 listCollections 操作的用户,此选项无效,因为该用户有权列出数据库中的集合。

不与 nameOnly: true 一起使用时,此选项无效。也就是说,在实施访问控制期间,用户必须具有运行命令所需的特权。否则,用户无权运行命令。

comment
注意到

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

使用筛选器限制listCollections 的结果。您可以对filter 结果集中 listCollections返回的任何字段 指定 。

listCollections 锁行为:

  • MongoDB 5.0 以前的版本,当 listCollections 持有数据库上的意向共享锁时,listCollections 会在数据库中的每个集合上获取意向共享锁

  • 从 MongoDB 5.0 开始,listCollections 不再对集合或数据库采用意图共享锁。listCollections 不会被对集合持有独占写锁的操作阻止。

要了解锁,请参阅MongoDB 使用什么类型的锁?。

从 MongoDB4 开始。2 ,如果发出 的客户端在操作完成之前断开连接,MongoDBlistCollections 会使用listCollections killOp将 标记为终止。

要在副本集成员上运行, listCollections操作要求该成员处于PRIMARYSECONDARY状态。如果节点处于其他状态,例如STARTUP2 ,则操作错误。

强制执行访问控制时, listCollections命令需要执行listCollections操作。用户必须具有授予对数据库执行listCollections操作的特权才能运行listCollections

例如,以下命令授予对 test 数据库运行 db.getCollectionInfos() 的特权:

{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }

内置角色 read 提供为特定数据库运行 listCollections 的特权。

authorizedCollectionsnameOnly 都设置为 true 时,没有所需的 read 特权的用户可以运行 listCollections。在这种情况下,该命令将返回用户对其拥有特权的集合的名称和类型。

例如,假设某个用户具有授予以下 find 特权的角色:

{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }

如果 authorizedCollectionsnameOnly 都设置为 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
}
)

mongosh 方法 show collections 类似于:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)
  • 对于具有所需访问权限的用户,show collections 将列出数据库的非系统集合。

  • 对于不具有所需访问权限的用户,show collections 仅列出用户对其拥有特权的集合。

listCollections.cursor

一个文档,其中包含创建具有集合名称和选项的文档游标所使用的信息。游标信息包括游标 ID、命令的完整命名空间以及第一批结果。批处理输出中的每个文档都包含以下字段:

字段
类型
说明
名称
字符串
集合的名称。
类型
字符串
数据存储类型。为collection 集合 返回 ,为view timeseries视图 返回 ,为 时间序列集合 返回 。
选项
文档

集合选项。

这些选项与 db.createCollection() 中的选项直接对应。有关选项的说明,请参阅 db.createCollection()

信息
文档

列出与集合相关的以下字段:

只读
boolean。如果为 true,则数据存储为只读。
uuid

UUID 。建立后,集合 UUID 就不会更改。集合 UUID 在分片集群的副本集成员和分片中保持不变。

版本 3.6 中的新增功能

idIndex
文档
提供有关集合的 _id 索引信息。
listCollections.ok

命令的返回值。值为 1 表示成功。

music 数据库包含三个集合:motorheadtaylorSwiftramones

要列出数据库中的集合,可以使用内置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
}

对于集合选项:

有关集合信息:

← killOp