Navigation

db.collection.validate()

On this page

  • Description
  • Syntax
  • Behavior
  • Examples
db.collection.validate(<documents>)
Important
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

For MongoDB API drivers, refer to the language specific MongoDB driver documentation.

Validates a collection. The method scans a collection data and indexes for correctness and returns the result. For details of the output, see Validate Output.

Starting in version 5.0, the db.collection.validate() method can also fix inconsistencies in the collection.

Index inconsistencies include:

  • An index is multikey but there are no multikey fields.
  • An index has multikeyPaths covering fields that are not multikey.
  • An index does not have multikeyPaths but there are multikey documents (for indexes built before 3.4).

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.

The db.collection.validate() method is a wrapper around the validate command.

Note
Changed in version 4.4

Changed in version 5.0.

The db.collection.validate() method has the following syntax:

db.collection.validate( {
full: <boolean>, // Optional
repair: <boolean> // Optional, added in MongoDB 5.0
} )

The db.collection.validate() method can take the following optional document parameter with the fields:

Field
Type
Description
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 with the following exception:

    • Starting in MongoDB 4.4, full validation on the oplog for WiredTiger skips the more thorough check.
  • If false, omits some checks for a faster but less thorough check.

The default is false.

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.

boolean

Optional. A flag that determines whether the command performs a repair.

  • If true, a repair is performed.
  • If false, no repair is performed.

The default is false.

A repair can only be run on a standalone node.

The repair fixes these issues:

  • If missing index entries are found, the missing keys are inserted into the index.
  • If extra index entries are found, the extra keys are removed from the index.
  • If multikey documents are found for an index that is not a multikey index, the index is changed to a multikey index.
  • If multikey documents are found that are not specified by an index's multikey paths, the index's multikey paths are updated.
  • If corrupt documents with invalid BSON data are found, the documents are removed.
Tip
See also:

New in version 5.0.

The db.collection.validate() method is potentially resource intensive and may impact the performance of your MongoDB instance, particularly on larger data sets.

The db.collection.validate() method obtains an exclusive lock on the collection. This will block all reads and writes on the collection until the operation finishes. When run on a secondary, the operation can block all other operations on that secondary until it finishes.

Warning

Due to the performance impact of validation, consider running db.collection.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.

Starting in version MongoDB 4.4,

  • To validate a collection myCollection using the default validation setting (specifically, full: false):

    db.myCollection.validate()
    db.myCollection.validate({ })
    db.myCollection.validate( { full: false } )
  • To perform a full validation of collection myCollection, specify full: true:

    db.myCollection.validate( { full: true } )
  • To repair collection myCollection, specify repair: true:

    db.myCollection.validate( { repair: true } )

For details of the output, see Validate Output.

Give Feedback

On this page

  • Description
  • Syntax
  • Behavior
  • Examples