Database Manual / Reference / mongosh Methods / Collections

db.collection.validate() (mongosh method方法)

Description描述

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

db.collection.validate(<documents>)

Validates a collection. The method scans a collection data and indexes for correctness and returns the result. For details of the output, see Validate Output.验证集合。该方法扫描集合数据和索引的正确性,并返回结果。有关输出的详细信息,请参阅验证输出

Starting in version 5.0, the db.collection.validate() method can also fix inconsistencies in the collection.从5.0版本开始,db.collection.validate()方法还可以修复集合中的不一致。

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

  • An index is multikey but there are no multikey fields.索引是多键的,但没有多键字段。
  • 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.还验证任何违反集合模式验证规则的文档。

The db.collection.validate() method is a wrapper around the validate command.db.collection.validate()方法是validate命令的包装器。

Compatibility兼容性

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

Syntax语法

The db.collection.validate() method has the following syntax:db.collection.validate()方法具有以下语法:

db.collection.validate( {
full: <boolean>, // Optional
repair: <boolean>, // Optional, added in MongoDB 5.0
checkBSONConformance: <boolean> // Optional, added in MongoDB 6.2
} )

Parameters参数

The db.collection.validate() method can take the following optional document parameter with the fields:db.collection.validate()方法可以在字段中接受以下可选文档参数:

Field字段Type类型Description描述
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.WiredTiger的oplog上的完整验证跳过了更彻底的检查。
  • 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中新增。

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命令找到非多键索引的multikey文档,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中新增。

checkBSONConformanceboolean布尔值

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

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

Behavior行为

Performance演出

The db.collection.validate() method is potentially resource intensive and may impact the performance of your MongoDB instance, particularly on larger data sets.db.collection.validate()方法可能是资源密集型的,可能会影响MongoDB实例的性能,特别是在较大的数据集上。

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

Warning

Validation has exclusive lock requirements that affect performance on primaries and on secondaries that are servicing reads. Consider only running db.collection.validate() on nodes that are not servicing reads or writes.验证具有独占锁要求,这会影响为读取提供服务的主服务器和辅助服务器的性能。考虑只在不提供读写服务的节点上运行db.collection.validate()

To minimize impact on the primary, the majority of the data-bearing (non-arbiter), voting members in the cluster must be available and must not have significant replication lag.为了尽量减少对主节点的影响,集群中的大多数数据承载(非仲裁器)投票成员必须可用,并且不得有明显的复制延迟。

To minimize the impact of the validation operation on client applications, run db.collection.validate() on a secondary node that is not servicing read requests. You can convert the current primary node to a secondary node, by running the rs.stepDown() method.为了尽量减少验证操作对客户端应用程序的影响,请在不处理读取请求的辅助节点上运行db.collection.validate()。您可以通过运行rs.stepDown()方法将当前主节点转换为辅助节点。

To completely isolate the db.collection.validate() operation from client traffic, choose one of the following options:要将db.collection.validate()操作与客户端流量完全隔离,请选择以下选项之一:

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检查。

Examples示例

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

    db.myCollection.validate()

    db.myCollection.validate({ })

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

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

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

    db.myCollection.validate( { checkBSONConformance: true } )

For details of the output, see Validate Output.有关输出的详细信息,请参阅验证输出