Navigation
This is an upcoming (i.e. in progress) version of the manual.

db.collection.validate()

Description

db.collection.validate(<document>)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.

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.

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

Syntax

Changed in version 4.4

db.collection.validate() no longer accepts just a boolean parameter. See db.collection.validate() Parameter Change.

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

db.collection.validate( {
   full: <boolean>,         // Optional
   background: <boolean>    // Optional.  New in 4.4
} )

Parameters

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

Field Type Description
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. You cannot run the more thorough validation with background: true.
  • 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.

background boolean

Optional. A flag that determines whether the command runs in the background.

  • If true, runs in the background. Background validation does not block reads and writes and DDL operations but does not update collection count and size metrics. You cannot run background validation with full: true.
  • If false, runs in the foreground. Foreground validation blocks reads and writes and DDL operations but does update the collection count and size metrics. When run on a secondary, the operation can block all other operations on that secondary until it finishes.

The default is false.

New in version 4.4.

Behavior

The db.collection.validate() method can be slow, particularly on larger data sets.

The db.collection.validate() method is potentially resource intensive and may impact the performance of your MongoDB instance. However, when run with background: true, you can limit the I/O and CPU usage per command by setting the maxValidateMBperSec parameter for the mongod instance.

Background Validation

Starting in version 4.4, you can run validation in the background for the WiredTiger storage engine. When run in background mode, the validation only checks data that has been checkpointed.

If a collection:

  • Has not yet been checkpointed, then the background validation returns successfully but includes a warning; i.e. the output includes ok: 1, valid: true, and a warning in the warnings array.
  • Has been checkpointed but is dropped during the validation, background validation returns an error in the validate.errors field.

If an index:

  • Has not yet been checkpointed, then background validation ignores the index.

  • Has been checkpointed and then dropped, but the drop has not yet been checkpointed, then background validation ignores the index unless the drop occurs while the validation process is checking the index.

    If the drop occurs while the validation process is checking the index, then background index returns an error in the validate.errors field.

For more information on WiredTiger and checkpoints, see Snapshots and Checkpoints.

Tip

When run with background: true, you can limit the I/O and CPU usage per command by setting the maxValidateMBperSec parameter for the mongod instance.

Locks

Starting in MongoDB 4.4, for the WiredTiger storage engine, you can run the validate command in the background or in the foreground. In earlier versions, you can only run validate in the foreground:

  • When run in background, the operation takes an intent shared lock on the collection and does not block reads and writes or DDL operations on the collection; however, the operation does not update collection count and size metrics.
  • When run in foreground, the operation takes an exclusive lock on the collection and blocks all reads and writes and DDL operations 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.

Examples

  • To validate a collection myCollection using the default settings (i.e. { full: false, background: false } )

    db.myCollection.validate()
    
    db.myCollection.validate({ })
    
    db.myCollection.validate( { full: false, background: false } )
    
  • To perform a full validation of collection myCollection, specify full: true. If you specify the background option, you must set it to false.

    db.myCollection.validate( { full: true } )
    
    db.myCollection.validate( { full: true, background: false } )
    

For details of the output, see Validate Output.