db.collection.validate()
On this page本页内容
Description描述
Changed in version 6.2.6.2版更改。
db.collection.validate(<documents>)
- Important
mongosh Method
This page documents a本页记录了一个mongosh
method.mongosh
方法。This is not the documentation for database commands or language-specific drivers, such as Node.js.这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the
validate
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release: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从版本5.0开始,db.collection.validate()
method can also fix inconsistencies in the collection.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.索引具有覆盖非多键字段的多键路径。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 totrue
.db.collection.validate()
命令检测到任何不一致,则会返回一个警告,并将索引上的修复标志设置为true
。db.collection.validate()
also validates any documents that violate the collection's schema validation rules.还验证任何违反集合的架构验证规则的文档。Thedb.collection.validate()
method is a wrapper around thevalidate
command.db.collection.validate()
方法是validate
命令的包装器。
Syntax语法
Changed in version 4.4
db.collection.validate()
no longer accepts just a boolean parameter. See db.collection.validate() Parameter Change.不再只接受布尔参数。请参见db.collection.validate()
参数更改。
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()
方法可以在字段中采用以下可选文档参数:
full | boolean |
false .false 。full validation process will force a checkpoint and flush all in-memory data to disk before verifying the on-disk data.full 验证过程才会强制执行检查点,并在验证磁盘上的数据之前将所有内存中的数据刷新到磁盘。 |
repair | boolean |
false .false 。
|
checkBSONConformance | boolean | true , the collection is checked to ensure the BSON documents conform to the BSON specifications. true ,则检查集合以确保BSON文档符合BSON规范。checkBSONConformance :
|
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. db.collection.validate()
方法获取集合的独占锁。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.这将阻止对集合的所有读取和写入,直到操作完成。在辅助设备上运行时,该操作可以阻止该辅助设备上的所有其他操作,直到完成为止。
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. db.collection.validate()
。You can convert the current primary node to a secondary node, by running the 可以通过运行rs.stepDown()
method.rs.stepDown()
方法将当前主节点转换为辅助节点。
To completely isolate the 要将db.collection.validate()
operation from client traffic, choose one of the following options:db.collection.validate()
操作与客户端流量完全隔离,请选择以下选项之一:
Isolate a replica set member by following the rolling maintenance procedure to temporarily remove it from the cluster.按照滚动维护过程隔离复制副本集成员,以便将其从群集中临时删除。Convert a secondary node to a replica set hidden member and perform the validation on the hidden node.将辅助节点转换为副本集隐藏成员,并对隐藏节点执行验证。
Data Throughput Metrics数据吞吐量指标
Starting in version MongoDB 4.4,从MongoDB 4.4版本开始,
The$currentOp
and thecurrentOp
command includedataThroughputAverage
anddataThroughputLastSecond
information for validate operations in progress.$currentOp
和currentOp
命令包括用于验证正在进行的操作的dataThroughputAverage
和dataThroughputLastSecond
信息。The log messages for validate operations include验证操作的日志消息包括dataThroughputAverage
anddataThroughputLastSecond
information.dataThroughputAverage
和dataThrouughputLastSecond
信息。
Collection Validation Improvements集合验证改进
Starting in MongoDB 6.2, the 从MongoDB 6.2开始,validate
command and db.collection.validate()
method: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.有关输出的详细信息,请参阅验证输出。