On this page本页内容
validate
The validate
command checks a collection's data and indexes for correctness and returns the results.validate
命令检查集合的数据和索引是否正确,并返回结果。
Starting in version 5.0, the 从5.0版开始,validate
command can also find inconsistencies in the collection and fix them if possible.validate
命令还可以发现集合中的不一致,并在可能的情况下修复它们。
Index inconsistencies include:索引不一致包括:
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 in mongosh
provides a wrapper around validate
.mongosh
中的db.collection.validate()
方法提供了一个围绕validate
的包装。
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()
。
The command takes the following fields:该命令接受以下字段:
validate | string | |
full | boolean |
|
repair | boolean |
|
metadata | boolean |
db.runCommand( { collMod: <collectionName> } )
|
The validate
command can be slow, particularly on larger data sets.validate
命令可能很慢,尤其是在较大的数据集上。
The validate
command obtains an exclusive lock W
on the collection. validate
命令获取集合的独占锁W
。This 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
操作可以阻止该辅助服务器上的所有其他操作,直到其完成。
Due to the performance impact of validation, consider running 由于验证对性能的影响,请考虑仅在secondary副本集节点上运行validate
only on secondary replica set nodes. validate
。You can use 您可以使用rs.stepDown()
to instruct the current primary node to become a secondary to avoid impacting a live primary node.rs.stepDown()
指示当前primary节点成为辅助节点,以避免影响活动的主节点。
Starting in version MongoDB 4.4,从MongoDB 4.4版开始,
$currentOp
and the currentOp
command include dataThroughputAverage
and dataThroughputLastSecond
information for validate operations in progress.$currentOp
和currentOp
命令包括用于验证正在进行的操作的dataThroughputAverage
和dataThroughputLastSecond
信息。dataThroughputAverage
and dataThroughputLastSecond
information.dataThroughputAverage
和dataThroughputLastSecond
信息。MongoDB drivers automatically set afterClusterTime for operations associated with causally consistent sessions. MongoDB驱动程序自动为与因果一致会话相关的操作设置afterClusterTime
。Starting in MongoDB 4.2, the 从MongoDB 4.2开始,validate
command no longer supports afterClusterTime. validate
命令不再支持afterClusterTime
。As such, 因此,validate
cannot be associated with causally consistent sessions.validate
不能与因果一致的会话相关联。
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 } )
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.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+), 从MongoDB 4.2(以及4.0.10+和3.6.13+)开始,keysPerIndex
identifies the index by its name only. keysPerIndex
仅通过其名称标识索引。Earlier versions of MongoDB displayed the full namespace of the index; i.e. 早期版本的MongoDB显示了索引的完整名称空间;即<db>.<collection>.$<index_name>
<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
indexDetails
<db>.<collection>.$<index_name>
.<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.valid
为false
),则此字段将包含描述验证错误的消息。
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>, ... } } ... ]
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>, ... } } ... ]
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,其中字段大小包括idKey
和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.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中新增。