validate

On this page本页内容

Definition定义

validate

The validate command checks a collection's data and indexes for correctness and returns the results.validate命令检查集合的数据和索引是否正确,并返回结果。

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.索引是多关键字,但没有多关键字字段。
  • An index has multikeyPaths covering fields that are not multikey.索引具有覆盖非多关键字字段的多关键字路径
  • An index does not have multikeyPaths but there are multikey documents (for indexes built before 3.4).索引没有多关键字路径,但有多关键字文档(用于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的包装。

Syntax语法

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

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

mongosh also provides a wrapper db.collection.validate().mongosh还提供了一个包装器db.collection.validate()

Command Fields命令字段

The command takes the following fields:该命令接受以下字段:

Field字段Type类型Description描述
validatestringThe 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,则执行更彻底的检查,但出现以下异常:

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

The default is false.默认值为false

Starting in MongoDB 3.6, 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.从MongoDB 3.6开始,对于WiredTiger存储引擎,只有full验证过程才会强制检查点,并在验证磁盘数据之前将所有内存数据刷新到磁盘。

In previous versions, the data validation process for the WT storage engine always forces a checkpoint.在以前的版本中,WT存储引擎的数据验证过程总是强制执行检查点。

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.如果为假,则不执行修复。

The default is false.默认值为false

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

The repair fixes these 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 multikey documents are found for an index that is not a multikey index, the index is changed to a multikey index.如果找到非multikey索引的索引的多键文档,则该索引将更改为multikey索引。
  • If multikey documents are found that are not specified by an index's multikey paths, the index's multikey paths are updated.如果找到索引的multikey路径未指定的multikey文档,则会更新索引的multikey路径。
  • If corrupt documents with invalid BSON data are found, the documents are removed.如果发现具有无效BSON数据的损坏文档,将删除这些文档。
Tip提示
See also: 参阅:

--repair option for mongodmongod--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.如果为true,则执行元数据验证扫描。
  • 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 }运行validate命令。

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中新增

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. 由于验证对性能的影响,请考虑仅在secondary副本集节点上运行validateYou can use rs.stepDown() to instruct the current primary node to become a secondary to avoid impacting a live primary node.您可以使用rs.stepDown()指示当前primary节点成为辅助节点,以避免影响活动的主节点。

Data Throughput Metrics数据吞吐量指标

Starting in version MongoDB 4.4,从MongoDB 4.4版开始,

Restrictions限制

MongoDB drivers automatically set afterClusterTime for operations associated with causally consistent sessions. MongoDB驱动程序自动为与因果一致会话相关的操作设置afterClusterTimeStarting in MongoDB 4.2, the validate command no longer supports afterClusterTime. 从MongoDB 4.2开始,validate命令不再支持afterClusterTimeAs such, validate cannot be associated with causally consistent sessions.因此,validate不能与因果一致的会话相关联。

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

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.nInvalidDocuments

The number of invalid documents in the collection.集合中无效文档的数量。

validate.nrecords

The number of documents in the collection.集合中的文档数。

validate.nIndexes

The number of indexes on the collection.集合上的索引数。

validate.keysPerIndex

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

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

Starting in MongoDB 4.2 (and 4.0.10+ and 3.6.13+), keysPerIndex identifies the index by its name only. 从MongoDB 4.2(以及4.0.10+和3.6.13+)开始,keysPerIndex仅通过其名称标识索引。Earlier versions of MongoDB displayed the full namespace of the index; i.e. <db>.<collection>.$<index_name>早期版本的MongoDB显示了索引的完整名称空间;即<db>.<collection>.$<index_name>

validate.indexDetails

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

"indexDetails" : {
   "_id_" : {
      "valid" : <boolean>
   },
   "<index2_name>" : {
      "valid" : <boolean>
   },
   ...
}

Starting in MongoDB 4.2 (and 4.0.10+ and 3.6.13+),从MongoDB 4.2(以及4.0.10+和3.6.13+)开始,

  • 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>
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. 如果validate确定集合的所有方面都有效,则为true的布尔值。When false, see the errors field for more information.如果为false,请参阅errors字段以了解更多信息。

validate.repaired

A boolean that is true if validate repaired the collection.如果validate修复了集合,则为true的布尔值。

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. 对于extraIndexEntries数组,所有indexKey字段大小的总和限制为1MB,其中大小包括indexKey的键和值。If the sum exceeds this size, the warning field displays a message.如果总和超过此大小,警告字段将显示一条消息。

Available starting in MongoDB 4.2 (and 4.0.10+ and 3.6.13+)从MongoDB 4.2(以及4.0.10+和3.6.13+)开始提供

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.如果总和超过此大小,警告字段将显示一条消息。

Available starting in MongoDB 4.2 (and 4.0.10+ and 3.6.13+)从MongoDB 4.2(以及4.0.10+和3.6.13+)开始提供

validate.corruptRecords

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

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

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

validate.ok

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

←  topvalidateDBMetadata →