定义
8.0.10版本新增。
警告
$listClusterCatalog
聚合阶段不受支持,也不能保证在未来版本中保持稳定。 不要构建依赖于此阶段的特定输出格式的功能,因为输出可能会在将来的版本中发生变化。
$listClusterCatalog
聚合阶段输出集群中集合的信息,包括名称和创建选项。
$listClusterCatalog
必须是管道的第一阶段。
您必须针对数据库调用此阶段。当您针对 管理员数据库运行此阶段时,它会返回有关集群中所有集合的信息。当您对任何其他数据库运行它时,它会返回有关该数据库中所有集合的信息。
语法
该阶段采用以下语法:
{ $listClusterCatalog: { shards: true, balancingConfiguration: true } }
$listClusterCatalog
阶段接受包含以下可选字段的文档:
字段 | 说明 |
---|---|
| 可选。您可以在调用 |
| 可选。如果您在调用 |
输出
$listClusterCatalog
为每个集合返回一个文档。每个文档可包含以下字段:
{ "ns" : <string>, "db" : <string>, "type" : <string>, "idIndex" : <document>, "options" : <document>, "info" : <document>, "sharded" : <boolean>, "shardKey" : <document>, "shards" : [<string>], "balancingEnabled" : <boolean>, "balancingEnabledReason" : <document>, "autoMergingEnabled" : <boolean>, "chunkSize" : <int> }
下表包含有关 $listClusterCatalog
返回的字段的信息:
字段 | 类型 | 随默认查询一起返回 | 说明 |
---|---|---|---|
| 字符串 | true | 集合的完整命名空间空间,格式为 |
| 字符串 | true | 集合所在数据库的名称。 |
| 字符串 | true | |
| 文档 | true | 提供有关集合的 |
| 文档 | true | 返回的文档包含以下字段:
这些选项直接对应于 |
| 文档 | true | 列出与集合相关的以下字段:
|
| 布尔 | true | 指定集合是分片的还是未分片的。该字段也存在于副本集服务器上。 |
| 文档 | 仅在集合分片的出现 | 集合的分片键。 |
| 字符串数组 | false,必须在输入文档中指定分片 | 每个集合的分片数。 |
| 布尔 | false,必须在输入文档中指定 balanceConfiguration | 指定是否为该集合启用负载均衡。仅当集合已分片的,此字段才会出现。 |
| 文档 | false,必须在输入文档中指定 balanceConfiguration | 返回有关使用哪个命令切换负载均衡的信息。该文档包含以下字段:
仅当集合已分片的,此字段才会出现。 |
| 布尔 | false,必须在输入文档中指定 balanceConfiguration | 指定是否为集合启用AutoMerger。仅当集合已分片的,此字段才会出现。 |
| int | false,必须在输入文档中指定 balanceConfiguration | 返回集合的数据块大小(以MiB为单位)。仅当集合已分片的,此字段才会出现。 |
限制
如果对 admin
数据库执行此命令,则需要 listClusterCatalog
权限动作,该动作包含在 clusterMonitor
角色中。
要对特定数据库运行此命令,您需要 listCollections
权限动作,该操作包含在 read
角色中。
示例
列出所有集合的信息
以下示例从 sample_mflix
数据库中的所有集合返回默认信息:
use sample_mflix db.aggregate([ { $listClusterCatalog: {} } ])
运行此示例时,它会返回一个大量,其中包含 sample_mflix
数据库中每个集合的文档。每个文档都包含上表中描述的默认字段:
[ { ns: "sample_mflix.movies", db: "sample_mflix", type: "collection", idIndex: { v: 2, key: { _id: 1 }, name: '_id' }, options: { ... }, sharded: false, info: { readOnly: false, uuid: new UUID("6c46c8b9-4999-4213-bcef-9a36b0cff228") } }, { ns: "sample_mflix.comments", db: "sample_mflix", type: "collection", options: { ... }, sharded: true, info: { readOnly: true, uuid: new UUID("6c448eb9-4090-4213-bbaf-9a36bb7fc98e") } shardKey: { _id: 1} }, ... ]
均衡配置
您可以返回分片的的数据库中所有集合的默认字段和有关负载均衡配置的信息。
以下示例返回 sample_mflix
数据库的负载均衡配置:
use sample_mflix db.aggregate([ { $listClusterCatalog: { balancingConfiguration: true } } ])
前面的示例返回一个大量,其中包含每个集合的一个文档。其中每个文档都包含默认字段以及分片的集合的 balancingEnabled
、balancingEnabledReason
、autoMergingEnabled
和 chunkSize
字段。以下代码提供了此输出的示例:
[ { ns: "sample_mflix.movies", db: "sample_mflix", type: "collection", idIndex: { v: 2, key: { _id: 1 }, name: '_id' }, options: { ... }, sharded: false, ... }, { ns: "sample_mflix.comments", db: "sample_mflix", type: "collection", sharded: true, shardKey: { _id: 1}, balancingEnabled: true, balancingEnabledReason: { enableBalancing: true, allowMigrations: false }, autoMergingEnabled: false, chunkSize: 256, ... }, ... ]