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:

  1. db.runCommand( {
  2. validate: <string>, // Collection name
  3. full: <boolean> // Optional
  4. } )

The command takes the following fields:

FieldTypeDescriptionvalidatestringThe name of the collection to validate.fullbooleanOptional. 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 is false.

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():

  1. 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)
  1. db.runCommand( { validate: "myCollection" } )
  • To perform a full validation of collection myCollection
  1. 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 formdatabase.collection.
  • validate.nInvalidDocuments
  • The number of invalid documents in the collection.
  • validate.nrecords
  • The number of documents in the collection.
  • validate.nIndexes
  • The number of indexes on the collection.
  • validate.keysPerIndex
  • A document that contains the name and index entry count for eachindex on the collection.
  1. "keysPerIndex" : {
  2. "_id_" : <num>,
  3. "<index2_name>" : <num>,
  4. ...
  5. }

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>

  • validate.indexDetails
  • A document that contains the status of the index validation for eachindex.
  1. "indexDetails" : {
  2. "_id_" : {
  3. "valid" : <boolean>
  4. },
  5. "<index2_name>" : {
  6. "valid" : <boolean>
  7. },
  8. ...
  9. }

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 if validate determines thatall aspects of the collection are valid. When false, see theerrors field for more information.
  • validate.errors
  • If the collection is not valid (i.evalid 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.
  1. "extraIndexEntries" : [
  2. {
  3. "indexName" : <string>,
  4. "recordId" : <NumberLong>, // for the non-existent document
  5. "indexKey" : {
  6. "<key1>" : <value>,
  7. ...
  8. }
  9. }
  10. ...
  11. ]

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.
  1. "missingIndexEntries" : [
  2. {
  3. "indexName" : <string>,
  4. "recordId" : <NumberLong>,
  5. "idKey" : <_id key value>, // The _id value of the document. Only present if an ``_id`` index exists.
  6. "indexKey" : { // The missing index entry
  7. "<key1>" : <value>,
  8. ...
  9. }
  10. }
  11. ...
  12. ]

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 the ok field has a value of 0.