validate
Definition
validate
- The
validate
command checks a collection’s data andindexes for correctness and returns the results.
Note
The validate
command does not support views and raises an error when run against a view.
The command has the following syntax:
- db.runCommand( {
- validate: <string>, // Collection name
- full: <boolean> // Optional
- } )
The command takes the following fields:
FieldTypeDescriptionvalidate
stringThe name of the collection to validate.full
booleanOptional. A flag that determines whether the commandperforms a slower but more thorough check or a faster but less thorough check.
- If
true
, performs a more thorough check. - If
false
, omits some checks for a fater but lessthorough check.The default isfalse
.
Starting in MongoDB 3.6, for the WiredTiger storage engine, only thefull
validation process will force a checkpoint and flush allin-memory data to disk before verifying the on-disk data.
In previous versions, the data validation process for the WT storage enginealways forces a checkpoint.
The mongo
shell also provides a wrapperdb.collection.validate()
:
- db.collection.validate();
Behavior
The validate
command can be slow, particularly onlarger data sets.
The validate
command obtains an exclusive lock W
onthe collection. This will block all reads and writes on the collectionuntil the operation finishes. When run on a secondary, thevalidate
operation can block all other operations on thatsecondary until it finishes.
Restrictions
MongoDB drivers automatically set afterClusterTime for operations associated with causallyconsistent sessions. Starting in MongoDB 4.2, thevalidate
command no longer support afterClusterTime. As such, validate
cannot be associatdwith causally consistent sessions.
Examples
- To validate a collection
myCollection
using the default settings(i.e.full: false
)
- db.runCommand( { validate: "myCollection" } )
- To perform a full validation of collection
myCollection
- db.runCommand( { validate: "myCollection", full: true } )
Validate Output
Note
The output may vary depending on the version and specificconfiguration of your MongoDB instance.
Specify { full: true }
for more detailed output.
validate.
ns
- The full namespace name of the collection. Namespaces include thedatabase name and the collection name in the form
database.collection
.
validate.
nrecords
- The number of documents in the collection.
validate.
keysPerIndex
- A document that contains the name and index entry count for eachindex 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 nameonly. Earlier versions of MongoDB displayed the full namespace ofthe index; i.e. <db>.<collection>.$<index_name>
- "indexDetails" : {
- "_id_" : {
- "valid" : <boolean>
- },
- "<index2_name>" : {
- "valid" : <boolean>
- },
- ...
- }
Starting in MongoDB 4.2 (and 4.0.10+ and 3.6.13+),
indexDetails
identifies the specific index (orindexes) that is invalid. Earlier versions of MongoDB would markall indexes as invalid, if any of the indexes were invalid.indexDetails
identifies the index by its nameonly. Earlier versions of MongoDB displayed the full namespace ofthe index; i.e.<db>.<collection>.$<index_name>
.
validate.
valid
- A boolean that is
true
ifvalidate
determines thatall aspects of the collection are valid. Whenfalse
, see theerrors
field for more information.
validate.
errors
- If the collection is not valid (i.e
valid
is false), this field will contain a messagedescribing the validation error.
validate.
extraIndexEntries
- An array that contains information for each index entry that pointsto 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 allthe indexKey
field sizes has a limit of 1MB where the sizesinclude both the keys and values for the indexKey
. Ifthe 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.
missingIndexEntries
- An array that contains information for each document that is missingthe 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 ofthe idKey
field size and all its indexKey
field sizes hasa limit of 1MB where the field sizes include both the keys andvalues for the idKey
and indexKey
. If the sumexceeds this size, the warning field displays a message.
Available starting in MongoDB 4.2 (and 4.0.10+ and 3.6.13+)
validate.
ok
- An integer with the value
1
when the command succeeds. If thecommand fails theok
field has a value of0
.