定义
checkMetadataConsistency对集群、数据库或集合的分元数据分片一系列一致性检查。 该命令返回一个游标,其中包含找到的全部或批处理不一致结果。
提示
In
mongosh, this command can also be run through thedb.checkMetadataConsistency(),db.collection.checkMetadataConsistency(), orsh.checkMetadataConsistency()helper methods.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.在主要维护操作(例如升级和降级)后运行此命令,以检查目录的状态。
默认情况下,该命令不会检查索引在分片之间的一致性。 要检查索引,请设置
checkIndexes选项。7.0版本新增。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
注意
checkMetadataConsistency 命令仅在连接到 mongos 时才可执行。 mongod 不支持此命令。
语法
要检查整个集群的分片元数据不一致情况,请从
admin数据库运行命令。db.adminCommand( { checkMetadataConsistency: 1 } ) 要检查数据库是否存在分片元数据不一致的情况,请从数据库上下文中运行以下命令:
use cars db.runCommand( { checkMetadataConsistency: 1 } ) 要检查集合分片元数据是否不一致,请使用集合名称运行命令:
use library db.runCommand( { checkMetadataConsistency: "authors", } )
命令字段
字段 | 类型 | 说明 |
|---|---|---|
| 整数或字符串 | 指定要检查的集合。
|
| 布尔 | 设置该命令是否还检查分片元数据中的索引。 For more information, see Check Indexes. |
| 文档 | 配置返回游标。 |
| 整型 | 每个批次中包含的不一致结果的最大数量。 |
| 整型 | 可选。允许命令等待获取数据库锁的最长时间(以毫秒为单位)。 您也可以将此字段与 |
| 整型 | 9.0版本新增。 可选。确定命令对所检查集合的分片数据块元数据进行比较的彻底程度的数据块计数阈值。
默认值为 For more information, see Chunk Check Threshold. |
输出
对于在分片元数据中发现的每个不一致,checkMetadataConsistency 命令会返回一个带有文档的游标。要学习;了解更多信息,请参阅不一致类型。
返回文档包含以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 文档 | 带有不一致检查结果的游标。 |
| 整型 | 一个 64 位整数表示游标 ID。 使用 如果游标返回的 ID 为 |
| 字符串 | 已检查数据库和collection是否存在不一致。 |
| 阵列 | 元数据一致性检查的结果。 |
| 布尔 | 指示命令是否成功。 |
行为
批处理结果
checkMetadataConsistency命令分批返回结果。 要自定义批处理大小, batchSize选项:
var cur = db.runCommand( { checkMetadataConsistency: 1, cursor: { batchSize: 10 } } )
如果cursor.id字段大于 0,则可以与getMore命令一起使用来检索下一批处理结果。
检查索引
默认情况下, checkMetadataConsistency命令不检查索引。 要检查元数据一致性和索引,请使用checkIndexes选项:
db.runCommand( { checkMetadataConsistency: 1, checkIndexes: true } )
数据块检查阈值
默认情况下,checkMetadataConsistency 命令会对配置服务器、分片和内存数据块映射中的每个数据块的元数据执行完整比较。在拥有大量数据块的分片上,此比较可以增加命令持有其元数据锁的时间。大量数据块也会增加命令从配置服务器读取的数据量。
为了减少这种影响,将 performStrictChunkChecksIfBelowThreshold 设置为数据块计数。当分片上检查的集合的数据块数量小于此阈值时,该命令会执行完整的数据块比较。当计数达到或超过阈值时,该命令仅比较数据块总数和最高数据块版本:
db.runCommand( { checkMetadataConsistency: 1, performStrictChunkChecksIfBelowThreshold: 10000 } )
例子
使用runCommand()运行checkMetadataConsistency命令:
db.runCommand( { checkMetadataConsistency: 1 } )
示例输出:
{ cursor: { id: Long("0"), ns: "test.$cmd.aggregate", firstBatch: [ { type: "MisplacedCollection", description: "Unsharded collection found on shard different from database primary shard", details: { namespace: "test.authors", shard: "shard02", localUUID: new UUID("1ad56770-61e2-48e9-83c6-8ecefe73cfc4") } } ], }, ok: 1 }