Definition
validateThe
validatecommand checks a collection's data and indexes for correctness and returns the results.Tip
In the
mongoShell, this command can also be run through thevalidate()helper method.Helper methods are convenient for
mongousers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
Syntax
The command has the following syntax:
db.runCommand( { validate: <string>, // Collection name full: <boolean> // Optional. } )
Command Fields
The command takes the following fields:
Field | Type | Description |
|---|---|---|
| string | The name of the collection to validate. |
boolean | Optional. A flag that determines whether the command performs a slower but more thorough check or a faster but less thorough check.
The default is Starting in MongoDB 3.6, for the WiredTiger storage engine, only the
In previous versions, the data validation process for the WT storage engine always forces a checkpoint. |
Behavior
Performance
The validate command can be slow, particularly on
larger data sets.
The validate command obtains an exclusive lock W on
the collection. 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.
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.
Data Throughput Metrics
Starting in version MongoDB 4.4,
The
$currentOpand thecurrentOpcommand includedataThroughputAverageanddataThroughputLastSecondinformation for validate operations in progress.The log messages for validate operations include
dataThroughputAverageanddataThroughputLastSecondinformation.
Restrictions
MongoDB drivers automatically set afterClusterTime for operations associated with causally
consistent sessions. Starting in MongoDB 4.2, the
validate command no longer supports afterClusterTime. As such, validate cannot be
associated with causally consistent sessions.
Examples
To validate a collection
myCollectionusing the default settings (i.e.full: false)db.runCommand( { validate: "myCollection" } ) To perform a full validation of collection
myCollectiondb.runCommand( { validate: "myCollection", full: true } )
Validate Output
Note
The output may vary depending on the version and specific configuration of your MongoDB instance.
Specify full: true for more detailed output.
validate.nrecordsThe number of documents in the collection.
validate.keysPerIndexA 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+),
keysPerIndexidentifies the index by its name only. Earlier versions of MongoDB displayed the full namespace of the index; i.e.<db>.<collection>.$<index_name>
validate.indexDetailsA 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+),
indexDetailsidentifies 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.indexDetailsidentifies the index by its name only. Earlier versions of MongoDB displayed the full namespace of the index; i.e.<db>.<collection>.$<index_name>.
validate.nsThe full namespace name of the collection. Namespaces include the database name and the collection name in the form
database.collection.
validate.validA boolean that is
trueifvalidatedetermines that all aspects of the collection are valid. Whenfalse, see theerrorsfield for more information.
validate.warningsAn 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.errorsIf the collection is not valid (i.e
validis false), this field will contain a message describing the validation error.
validate.extraIndexEntriesAn 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
extraIndexEntriesarray, the sum of all theindexKeyfield sizes has a limit of 1MB where the sizes include both the keys and values for theindexKey. 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+)
validate.missingIndexEntriesAn 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
missingIndexEntriesarray, the sum of theidKeyfield size and all itsindexKeyfield sizes has a limit of 1MB where the field sizes include both the keys and values for theidKeyandindexKey. 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+)
validate.corruptRecordsAn array of
RecordIdvalues for documents that are unreadable, possibly because the data is damaged. These documents are reported as corrupt during validation. ARecordIdis a 64-bit integer internal key that uniquely identifies a document in a collection."corruptRecords" : [ NumberLong(1), // RecordId 1 NumberLong(2) // RecordId 2 ] New in version 4.4.7.
validate.okAn integer with the value
1when the command succeeds. If the command fails theokfield has a value of0.