Database Manual / Reference / Database Commands / Diagnostics

validate (database command数据库命令)

Definition定义

Changed in version 6.2.在版本6.2中的更改。

validate

The validate command checks a collection's data and indexes for correctness and returns the results. The command also repairs any inconsistencies in the count and data size of a collection.validate命令检查集合的数据和索引是否正确,并返回结果。该命令还修复集合的计数和数据大小中的任何不一致。

Tip

In mongosh, this command can also be run through the validate() helper method.mongosh中,此命令也可以通过validate()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。

Changed in version 5.0.在版本5.0中的更改。

Starting in version 5.0, the validate command can also find inconsistencies in the collection and fix them if possible.从5.0版本开始,validate命令还可以发现集合中的不一致,并在可能的情况下进行修复。

Index inconsistencies include:指数不一致包括:

  • An index is multikey but there are no multikey fields.索引是多键的,但没有multikey字段。
  • An index has multikeyPaths covering fields that are not multikey.索引具有覆盖非多键字段的multikeyPaths
  • An index does not have multikeyPaths but there are multikey documents (for indexes built before 3.4).索引没有multikeyPaths,但有多键文档(适用于3.4之前构建的索引)。

If any inconsistencies are detected by the db.collection.validate() command, a warning is returned and the repair flag on the index is set to true.如果db.collection.validate()命令检测到任何不一致,将返回警告,并将索引上的修复标志设置为true

db.collection.validate() also validates any documents that violate the collection's schema validation rules.还验证任何违反集合模式验证规则的文档。

Note

The validate command does not support views and raises an error when run against a view.validate命令不支持视图,并在对视图运行时引发错误。

The db.collection.validate() method in mongosh provides a wrapper around validate.mongosh中的db.collection.validate()方法提供了一个围绕validate的包装器。

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部署的完全托管服务

Important

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令

  • 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的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
validate: <string>, // Collection name
full: <boolean>, // Optional
repair: <boolean>, // Optional, added in MongoDB 5.0
metadata: <boolean>, // Optional, added in MongoDB 5.0.4
checkBSONConformance: <boolean> // Optional, added in MongoDB 6.2
background: <boolean> // Optional
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
validatestring字符串The name of the collection to validate.要验证的集合的名称。
fullboolean布尔值

Optional. 可选。A flag that determines whether the command performs a slower but more thorough check or a faster but less thorough check.一个标志,用于确定命令是执行较慢但更彻底的检查,还是执行较快但不太彻底的检查。

  • If true, performs a more thorough check with the following exception:如果为true,则执行更彻底的检查,但以下情况除外:

    • Full validation on the oplog for WiredTiger skips the more thorough check. The validate.warnings includes a notice of the behavior.WiredTiger的oplog上的完整验证跳过了更彻底的检查。validate.warnings包括行为通知。
  • If false, omits some checks for a faster but less thorough check.如果为false,则省略一些检查,以便进行更快但不太彻底的检查。

The default is false.默认值为false

For the WiredTiger storage engine, only the full validation process will force a checkpoint and flush all in-memory data to disk before verifying the on-disk data.对于WiredTiger存储引擎,只有full验证过程才会强制执行检查点,并在验证磁盘上的数据之前将所有内存中的数据刷新到磁盘。

repairboolean布尔值

Optional. A flag that determines whether the command performs a repair.可选。确定命令是否执行修复的标志。

  • If true, a repair is performed.如果为true,则进行维修。
  • If false, no repair is performed.如果为false,则不进行维修。

The default is false.默认值为false

A repair can only be run on a standalone node.修复只能在独立节点上运行。

The repair fixes the following issues:修复修复了以下问题:

  • If missing index entries are found, the missing keys are inserted into the index.如果发现缺少的索引条目,则将缺少的键插入索引中。
  • If extra index entries are found, the extra keys are removed from the index.如果发现额外的索引条目,则从索引中删除额外的键。
  • If corrupt documents with invalid BSON data are found, the documents are removed.如果发现具有无效BSON数据的损坏文档,则会删除这些文档。

IMPORTANT: To set repair to true, you must set the fixMultikey option to true.重要提示:要将repair设置为true,您必须将fixMultikey选项设置为true

For more information, see the --repair option for mongod有关更多信息,请参阅mongod--repair选项

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

metadataboolean布尔值

Optional. A flag which allows users to perform a quick validation to detect invalid index options without scanning all of the documents and indexes.可选。一个标志,允许用户执行快速验证以检测无效的索引选项,而无需扫描所有文档和索引。

  • If true, a metadata validation scan is performed.如果为真,则执行元数据验证扫描。
  • If false, no metadata validation scan is not performed.如果为false,则不执行元数据验证扫描。

The default is false.默认值为false

Running the validate command with { metadata: true } is not supported with any other validate options.任何其他validate选项都不支持使用{ metadata: true }运行验证命令。

The metadata validation option:metadata验证选项:

  • Provides you a faster way of identifying invalid indexes by scanning only collections metadata.通过仅扫描集合元数据,为您提供了一种更快的识别无效索引的方法。
  • Provides an alternative to dropping and recreating multiple invalid indexes when used with the collMod command.当与collMod命令一起使用时,提供了一种删除和重新创建多个无效索引的替代方法。

The metadata validation option only scans collection metadata to find invalid indexes more quickly.metadata验证选项仅扫描集合元数据,以更快地找到无效索引。

If there is an invalid index detected, the validate command will prompt you to use the collMod command to remove invalid indexes.如果检测到无效索引,validate命令将提示您使用collMod命令删除无效索引。

db.runCommand( { collMod: <collectionName> } )

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

checkBSONConformanceboolean布尔值

Optional. If true, the collection is checked to ensure the BSON documents conform to the BSON specifications. 可选。如果为true,则检查集合情况,以确保BSON文档符合BSON规范。The checks increase the time to complete the validation operation. Any issues are returned as a warning.这些检查增加了完成验证操作的时间。任何问题都将作为警告返回。

checkBSONConformance:

  • Default is false.默认值为false
  • Is enabled when full is set to true.full设置为true时启用。
  • Cannot be used with:不能与以下内容一起使用:

    • repair set to true.设置为true
    • metadata set to true.设置为true

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

fixMultikeyboolean布尔值

Optional. If true, MongoDB fixes the following issues:可选。如果为true,MongoDB会修复以下问题:

  • If the validate command finds multikey documents for a non-multikey index, MongoDB changes the index to a multikey index.如果validate命令找到非多键索引的多键文档,MongoDB会将索引更改为多键索引。
  • If the validate command finds multikey documents that aren't specified by an index's multikey paths, MongoDB updates index's multikey paths.如果validate命令发现索引的多键路径未指定的多键文档,MongoDB会更新索引的多键路径。

The default is false.默认值为false

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

Behavior行为

Performance性能

The validate command can be slow, particularly on larger data sets.validate命令可能很慢,特别是在较大的数据集上。

The validate command obtains an exclusive lock W on the collection. validate命令获取集合上的独占锁WThis will block all reads and writes on the collection until the operation finishes. 这将阻止对集合的所有读写操作,直到操作完成。When run on a secondary, the validate operation can block all other operations on that secondary until it finishes.在辅助服务器上运行时,validate操作可以阻止该辅助服务器上的所有其他操作,直到完成为止。

Warning

Due to the performance impact of validation, consider running validate only on secondary replica set nodes. You can use rs.stepDown() to instruct the current primary node to become a secondary to avoid impacting a live primary node.由于验证对性能的影响,请考虑仅在secondary副本集节点上运行validate。您可以使用rs.stepDown()命令当前primary节点成为secondary,以避免影响活动的primary节点。

Data Throughput Metrics数据吞吐量指标

The $currentOp and the currentOp command include dataThroughputAverage and dataThroughputLastSecond information for validate operations in progress.$currentOpcurrentOp命令包含dataThroughputAveragedataThroughputLastSecond信息,用于验证正在进行的操作。

The log messages for validate operations include dataThroughputAverage and dataThroughputLastSecond information.验证操作的日志消息包括dataThroughputAveragedataThroughputLastSecond信息。

Collection Validation Improvements集合验证改进

Starting in MongoDB 6.2, the validate command and db.collection.validate() method:从MongoDB 6.2开始,validate命令和db.collection.validate()方法:

  • Check collections to ensure the BSON documents conform to the BSON specifications.检查集合,确保BSON文档符合BSON规范。
  • Check time series collections for internal data inconsistencies.检查时间序列集合是否存在内部数据不一致。
  • Have a new option checkBSONConformance that enables comprehensive BSON checks.有一个新的选项checkBSONConformance,可以进行全面的BSON检查。

Restrictions限制

The validate command no longer supports afterClusterTime. As such, validate cannot be associated with causally consistent sessions.validate命令在afterClusterTime之后不再支持。因此,validate不能与因果一致的会话相关联。

Time series collections were introduced in MongoDB 5.0. Starting in v5.2, the default internal format for storing time series measurements changed. Due to this change:MongoDB 5.0引入了时间序列集合。从v5.2开始,存储时间序列测量值的默认内部格式发生了变化。由于这一变化:

  • Time series collections created before v5.2 might contain documents in both the old and new format. Internally, such collections are flagged as timeseriesBucketsMayHaveMixedSchemaData: true.v5.2之前创建的时间序列集合可能包含新旧格式的文档。在内部,这些集合被标记为timeseriesBucketsMayHaveMixedSchemaData:true
  • Time series collections created in v5.2 or later will always contain documents in the new format. Internally, such collections are flagged as timeseriesBucketsMayHaveMixedSchemaData: false or not flagged at all.在v5.2或更高版本中创建的时间序列集合将始终包含新格式的文档。在内部,这样的集合被标记为时间序列BucketsMayHaveMixedSchemaData:false或根本不标记。

When the flag is true, time series queries take both the new and the old format into account. When the flag is false or missing, time series queries take only the new format into account.当标志为true时,时间序列查询会同时考虑新格式和旧格式。当标志为false或缺失时,时间序列查询只考虑新格式。

Due to a bug described in SERVER-91194, under some conditions the flag might be lost. When this happens for time series collections created before v5.2, read query results may be incomplete. That is, some documents may be missed, even though they are still stored on the disk.由于SERVER-91194中描述的错误,在某些情况下,标记可能会丢失。当v5.2之前创建的时间序列集合发生这种情况时,读取查询结果可能不完整。也就是说,有些文档可能会丢失,即使它们仍然存储在磁盘上。

To determine if you are impacted by this, run validate on your time series collection. The command returns an error if the collection is affected by the bug. Your read query results may be incorrect if this is the case.要确定是否受此影响,请对时间序列集合运行validate。如果集合受错误影响,则该命令返回错误。如果是这种情况,您读取的查询结果可能不正确。

If affected, upgrade to a fixed version and set timeseriesBucketsMayHaveMixedSchemaData to true for each affected collection to ensure that future queries on the collection return correct results. 如果受影响,请升级到固定版本,并为每个受影响的集合将timeseriesBucketsMayHaveMixedSchemaData设置为true,以确保将来对该集合的查询返回正确的结果。The full steps for this process are located here.此过程的完整步骤位于此处

Index Key Format索引键格式

Starting in MongoDB 6.0, the validate command returns a message if a unique index has a key format that is incompatible. The message indicates an old format is used.从MongoDB 6.0开始,如果唯一索引的键格式不兼容,validate命令将返回一条消息。该消息表示使用了旧格式。

Count and Data Size Statistics计数和数据大小统计

The validate command updates the collection's count and data size statistics in the collStats output with their correct values.validate命令用正确的值更新collStats输出中的集合计数和数据大小统计信息。

Note

In the event of an unclean shutdown, the count and data size statistics might be inaccurate.如果发生不干净的关闭,计数和数据大小统计数据可能不准确。

Examples示例

  • To validate a collection myCollection using the default validation setting (specifically, full: false):要使用默认验证设置(特别是full:false)验证集合myCollection,请执行以下操作:

    db.runCommand( { validate: "myCollection" } )
  • To perform a full validation of collection myCollection, specify full: true:要对集合myCollection执行完整验证,请指定full:true

    db.runCommand( { validate: "myCollection", full: true } )
  • To repair collection myCollection, specify repair: true:要修复集合myCollection,请指定repair:true

    db.runCommand( { validate: "myCollection", repair: true } )
  • To validate the metadata in the myCollection collection, specify metadata: true:要验证myCollection集合中的元数据,请指定metadata:true

    db.runCommand( { validate: "myCollection", metadata: true } )
  • To perform additional BSON conformance checks in myCollection, specify checkBSONConformance: true:要在myCollection中执行其他BSON一致性检查,请指定checkBSONConformance:true

    db.runCommand( { validate: "myCollection", checkBSONConformance: true } )

Validate Output验证输出

Note

The output may vary depending on the version and specific configuration of your MongoDB instance.输出可能因MongoDB实例的版本和特定配置而异。

Specify full: true for more detailed output.指定full:true以获得更详细的输出。

validate.uuid

The universally unique identifier (UUID) for the collection.集合的通用唯一标识符(UUID)。

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

validate.nInvalidDocuments
The number of invalid documents in the collection. Invalid documents are those that are not readable, which means the BSON document is corrupt and has an error or a size mismatch.集合中无效文档的数量。无效文档是指不可读的文档,这意味着BSON文档已损坏,存在错误或大小不匹配。
validate.nNonCompliantDocuments

The number of documents not conforming to the collection's schema. Non-compliant documents are not counted as invalid in nInvalidDocuments.不符合集合架构的文档数。不符合要求的文档在nInvalidDocuments中不被视为无效。

Starting in MongoDB 6.2, nNonCompliantDocuments also includes the number of documents that do not conform to the BSON or time series collection requirements.从MongoDB 6.2开始,nNonComplianceDocuments还包括不符合BSON时间序列集合要求的文档数量。

validate.nrecords
The number of documents in the collection.集合中的文档数量
validate.nIndexes
The number of indexes on the collection that were validated.集合上已验证的索引数。
validate.keysPerIndex

A document that contains the name and index entry count for each index on the collection.包含集合上每个索引的名称和索引条目计数的文档。

"keysPerIndex" : {
"_id_" : <num>,
"<index2_name>" : <num>,
...
}

keysPerIndex identifies the index by its name only.仅通过索引名称标识索引。

validate.indexDetails

Changed in version 8.1.在版本8.1中的更改。

A document that contains the status of the index validation for each index and the index specification.包含每个索引的索引验证状态和索引规范的文档。

"indexDetails" : {
"_id_" : {
"valid" : <boolean>,
"spec" : <document>
},
"<index2_name>" : {
"valid" : <boolean>,
"spec" : <document>
},
...
}
  • indexDetails identifies the specific index (or indexes) that is invalid. Earlier versions of MongoDB would mark all indexes as invalid, if any of the indexes were invalid.标识无效的特定索引。如果任何索引无效,早期版本的MongoDB会将所有索引标记为无效。
  • indexDetails identifies the index by its name only. Earlier versions of MongoDB displayed the full namespace of the index; i.e. <db>.<collection>.$<index_name>.仅通过索引名称标识索引。早期版本的MongoDB显示了索引的完整命名空间;即<db>.<collection>.$<index_name>
  • The spec document is the index specification, which varies depending on how the index is defined. Some example spec document fields include:spec文档是索引规范,根据索引的定义而有所不同。一些示例spec文档字段包括:

    • spec.v. The index version..索引版本。
    • spec.unique. A Boolean value that indicates if the index is unique.。一个布尔值,指示索引是否唯一。
    • spec.key. The index key identifier.索引键标识符。
    • spec.name. The index name.。索引名称。

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

validate.ns
The full namespace name of the collection. Namespaces include the database name and the collection name in the form database.collection.集合的完整命名空间名称。命名空间包括数据库名称和表单database.collection中的集合名称。
validate.valid
A boolean that is true if validate determines that all aspects of the collection are valid. When false, see the errors field for more information.如果validate确定集合的所有方面都有效,则布尔值为true。如果为false,请参阅错误字段以获取更多信息。
validate.repaired
A boolean that is true if validate repaired the collection.一个布尔值,如果validate修复了集合,则为true
validate.repairMode

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

A string that indicates what types of data inconsistencies the validate command attempted to repair, if detected. 一个字符串,指示validate命令尝试修复哪些类型的数据不一致(如果检测到)。Possible repairMode values include:可能的repairMode值包括:

  • None: No repair actions are taken.:未采取任何维修行动。
  • FixErrors: Attempts to fix any validation errors.:尝试修复任何验证错误。
  • AdjustMultikey: Attempts to fix multikey inconsistencies by adjusting multikey metadata.:尝试通过调整多键元数据来修复多键不一致。
validate.warnings

An array that contains warning messages, if any, regarding the validate operation itself. The warning messages do not indicate that the collection is itself invalid. For example:一个数组,其中包含有关验证操作本身的警告消息(如果有的话)。警告消息并不表示集合本身无效。例如:

"warnings" : [
"Could not complete validation of table:collection-28-6471619540207520785. This is a transient issue as the collection was actively in use by other operations."
],
validate.errors
If the collection is not valid (i.e valid is false), this field will contain a message describing the validation error.如果集合无效(即validfalse),则此字段将包含一条描述验证错误的消息。
validate.extraIndexEntries

An array that contains information for each index entry that points to a document that does not exist in the collection.一个数组,包含指向集合中不存在的文档的每个索引条目的信息。

"extraIndexEntries" : [
{
"indexName" : <string>,
"recordId" : <NumberLong>, // for the non-existent document
"indexKey" : {
"<key1>" : <value>,
...
}
}
...
]

Note

For the extraIndexEntries array, the sum of all the indexKey field sizes has a limit of 1MB where the sizes include both the keys and values for the indexKey. If the sum exceeds this size, the warning field displays a message.对于extraIndexEntries数组,所有indexKey字段大小的总和限制为1MB,其中大小包括indexKey的键和值。如果总和超过此大小,警告字段将显示一条消息。

validate.missingIndexEntries

An array that contains information for each document that is missing the corresponding index entry.一个数组,包含缺少相应索引条目的每个文档的信息。

"missingIndexEntries" : [
{
"indexName" : <string>,
"recordId" : <NumberLong>,
"idKey" : <_id key value>, // The _id value of the document. Only present if an ``_id`` index exists.
"indexKey" : { // The missing index entry
"<key1>" : <value>,
...
}
}
...
]

Note

For the missingIndexEntries array, the sum of the idKey field size and all its indexKey field sizes has a limit of 1MB where the field sizes include both the keys and values for the idKey and indexKey. 对于missingIndexEntries数组,idKey字段大小与其所有indexKey字段大小之和的限制为1MB,其中字段大小包括idKeyindexKey的键和值。If the sum exceeds this size, the warning field displays a message.如果总和超过此大小,警告字段将显示一条消息。

validate.corruptRecords

An array of RecordId values for documents that are unreadable, possibly because the data is damaged. These documents are reported as corrupt during validation. 不可读文档的RecordId值数组,可能是因为数据已损坏。这些文档在验证过程中被报告为已损坏。A RecordId is a 64-bit integer internal key that uniquely identifies a document in a collection.RecordId是一个64位整数内部键,用于唯一标识集合中的文档。

"corruptRecords" : [
Long(1), // RecordId 1
Long(2) // RecordId 2
]

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

validate.ok
An integer with the value 1 when the command succeeds. If the command fails the ok field has a value of 0.命令成功时,值为1的整数。如果命令失败,ok字段的值为0