Database Manual / Reference / Database Commands / Sharding

checkMetadataConsistency (database command数据库命令)

Definition定义

checkMetadataConsistency

Performs a series of consistency checks on sharding metadata for a cluster, database, or collection. The command returns a cursor with either all or a batch of the inconsistency results found.对集群、数据库或集合的分片元数据执行一系列一致性检查。该命令返回一个游标,其中包含所有或一批发现的不一致结果

Tip

In mongosh, this command can also be run through the db.checkMetadataConsistency(), db.collection.checkMetadataConsistency(), or sh.checkMetadataConsistency() helper methods.mongosh中,此命令也可以通过db.checkMetadataConsistency()db.collection.checkMetadataConsistency()sh.checkMetadataConsistency()辅助方法运行。

Helper methods are convenient for mongosh users, 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.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。

Run this command after major maintenance operations, such as upgrades and downgrades, to check the state of the catalog.在主要维护操作(如升级和降级)后运行此命令,以检查目录的状态。

By default, the command does not check indexes for consistency across the shards. To check indexes, set the checkIndexes option.默认情况下,该命令不会检查分片之间的索引一致性。要检查索引,请设置checkIndexes选项。

New in version 7.0.在版本7.0中新增。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Note

The checkMetadataConsistency command is executable only when connecting to mongos. mongod does not support this command.checkMetadataConsistency命令仅在连接到mongos时可执行。mongod不支持此命令。

Syntax语法

  • To check the entire cluster for sharding metadata inconsistencies, run the command from the admin database.要检查整个集群是否存在分片元数据不一致,请从admin数据库运行该命令。

    db.adminCommand( {
    checkMetadataConsistency: 1
    } )
  • To check the database for sharding metadata inconsistencies, run the command from the database context:要检查数据库是否存在分片元数据不一致,请从数据库上下文运行以下命令:

    use cars
    db.runCommand( {
    checkMetadataConsistency: 1
    } )
  • To check a collection for sharding metadata inconsistencies, run the command with the collection name:要检查集合是否存在分片元数据不一致,请使用集合名称运行命令:

    use library
    db.runCommand( {
    checkMetadataConsistency: "authors",
    } )

Command Fields命令字段

Field字段Type类型Description描述
checkMetadataConsistencyinteger or string整数或字符串

Specifies the collection to check.指定要检查的集合。

1
Sets the command to check all collections in the database for metadata inconsistencies. 设置命令以检查数据库中的所有集合是否存在元数据不一致。If run on the admin database, it checks all collections in all databases.如果在admin数据库上运行,它将检查所有数据库中的所有集合。
"<collection>"
Sets the collection to check for metadata inconsistencies.设置集合以检查元数据不一致。
checkIndexesboolean布尔值

Sets whether the command also checks indexes in sharding metadata.设置该命令是否也检查分片元数据中的索引。

For more information, see Check Indexes.有关更多信息,请参阅检查索引

cursordocument文档Configures the return cursor.配置返回游标。
cursor.batchSizeinteger整数Maximum number of inconsistency results to include in each batch.每批中包含的不一致结果的最大数量。
dbMetadataLockMaxTimeMSinteger整数

Optional. 可选。Maximum time in milliseconds that the command is allowed to wait to acquire a database lock.允许命令等待获取数据库锁的最长时间(毫秒)。

You can also use this field with maxTimeMS. 您还可以将此字段与maxTimeMS一起使用。For example, if you set dbMetadataLockMaxTimeMs to 500 and maxTimeMs to 5000, the operation waits 500 milliseconds to try to acquire the database lock and waits a total of 5000 milliseconds for the entire operation to finish.例如,如果将dbMetadataLockMaxTimeMs设置为500,将maxTimeMs设为5000,则操作将等待500毫秒尝试获取数据库锁,并等待整个操作完成总共5000毫秒。

Output输出

The checkMetadataConsistency command returns a cursor with a document for each inconsistency found in sharding metadata. To learn more, see Inconsistency Types.checkMetadataConsistency命令为分片元数据中发现的每个不一致返回一个带有文档的游标。要了解更多信息,请参阅不一致类型

The return document has the following fields:

Field字段Type类型Description描述
cursordocument文档Cursor with the results of the inconsistency checks.游标显示不一致检查的结果。
cursor.idinteger整数

A 64-bit integer indicated the cursor ID. 一个64位整数表示游标ID。Use the cursor.id value with the getMore command to retrieve the next batch of inconsistencies.cursor.id值与getMore命令一起使用,以检索下一批不一致。

If the cursor returns an ID of 0, it indicates that there are no more batches of information.如果游标返回的ID为0,则表示没有更多批次的信息。

cursor.nsstring字符串The database and collection checked for inconsistencies.数据库和集合检查了不一致性。
cursor.firstBatcharray数组Results of metadata consistency checks.元数据一致性检查的结果。
okboolean布尔值Indicates whether the command was successful.指示命令是否成功。

Behavior行为

Batch Results批量结果

The checkMetadataConsistency command returns results in batches. To customize the batch size, the batchSize option:checkMetadataConsistency命令分批返回结果。要自定义批大小,请使用batchSize选项:

var cur = db.runCommand( {
checkMetadataConsistency: 1,
cursor: {
batchSize: 10
}
} )

If the cursor.id field is greater than 0, you can use with the getMore command to retrieve the next batch of results.如果cursor.id字段大于0,则可以使用getMore命令检索下一批结果。

Check Indexes检查索引

The checkMetadataConsistency command does not check indexes by default. To check metadata consistency and indexes, use the checkIndexes option:默认情况下,checkMetadataConsistency命令不检查索引。要检查元数据一致性和索引,请使用checkIndexes选项:

db.runCommand( {
checkMetadataConsistency: 1,
checkIndexes: true
} )

Example示例

Use runCommand() to run the checkMetadataConsistency command:使用runCommand()运行checkMetadataConsistency命令:

db.runCommand( { checkMetadataConsistency: 1 } )

Example Output:输出示例:

{
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
}