validate

Definition

validate

The validate command checks a collection’s data and indexes for correctness and returns the results.

Note

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:

Field Type Description
validate string The name of the collection to validate.
full boolean

Optional. A flag that determines whether the command performs 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 faster but less thorough check.

Defaults to false

Changed in version 3.6.

Starting in MongoDB 3.6, for the WiredTiger storage engine, only the full validation process will force a checkpoint and flush all in-memory data to disk before verifying the on-disk data.

In previous versions, the data validation process for the WT storage engine always forces a checkpoint.

The mongo shell also provides a wrapper db.collection.validate():

db.collection.validate();

Behavior

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.

Note

Due to the manner in which validate scans data structures, even a full collection validation cannot detect all forms of corruption on MMAPv1 storage engine data files.

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

validate produces different output depending on which storage engine you are using. Specify {validate: "<collName>",  full: true } for more detailed output.

The following fields are common to the MMAPv1 and WiredTiger storage engines:

validate. ns

The full namespace name of the collection. Namespaces include the database name and the collection name in the form database.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 each index on the collection.

"keysPerIndex" : {
   "_id_" : <num>,
   "<index2_name>" : <num>,
   ...
}

Starting in 3.6.13+, keysPerIndex identifies the index by its name only. Earlier versions of MongoDB displayed the full namespace of the index; i.e. <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 3.6.13+,

  • indexDetails identifies 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.
  • indexDetails identifies the index by its name only. Earlier versions of MongoDB displayed the full namespace of the index; i.e. <db>.<collection>.$<index_name>.
validate. valid

A boolean that is true if validate determines that all aspects of the collection are valid. When false, see the errors field for more information.

validate. errors

If the collection is not valid (i.e valid is false), this field will contain a message describing the validation error.

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>,
         ...
      }
   }
   ...
]

Note

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. If the sum exceeds this size, the warning field displays a message.

Available starting in MongoDB 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>,
         ...
      }
   }
   ...
 ]

Note

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. If the sum exceeds this size, the warning field displays a message.

Available starting in MongoDB 3.6.13+

validate. ok

An integer with the value 1 when the command succeeds. If the command fails the ok field has a value of 0.

The following fields are specific to MMAPv1:

validate. firstExtent

The disk location of the first extent in the collection. The value of this field also includes the namespace.

validate. lastExtent

The disk location of the last extent in the collection. The value of this field also includes the namespace.

validate. extentCount

The number of extents in the collection.

validate. extents

validate returns one instance of this document for every extent in the collection. This embedded document is only returned when you specify the full option to the command.

validate.extents. loc

The disk location for the beginning of this extent.

validate.extents. xnext

The disk location for the extent following this one. “null” if this is the end of the linked list of extents.

validate.extents. xprev

The disk location for the extent preceding this one. “null” if this is the head of the linked list of extents.

validate.extents. nsdiag

The namespace this extent belongs to (should be the same as the namespace shown at the beginning of the validate listing).

validate.extents. size

The number of bytes in this extent.

validate.extents. firstRecord

The disk location of the first record in this extent.

validate.extents. lastRecord

The disk location of the last record in this extent.

validate. datasize

The number of bytes in all data records. This value does not include deleted records, nor does it include extent headers, nor record headers, nor space in a file unallocated to any extent. datasize includes record padding.

validate. lastExtentSize

The size of the last new extent created in this collection. This value determines the size of the next extent created.

validate. padding

A floating point value between 1 and 2.

When MongoDB creates a new record it uses the padding factor to determine how much additional space to add to the record. The padding factor is automatically adjusted by mongo when it notices that update operations are triggering record moves.

validate. firstExtentDetails

The size of the first extent created in this collection. This data is similar to the data provided by the extents embedded document; however, the data reflects only the first extent in the collection and is always returned