validate()

The validate command is used to examine a MongoDB collection to verify and report on the correctness of its internal structures, such as indexes, namespace details, or documents. This command can also return statistics about the storage and distribution of data within a collection.

Usage

The basic syntax of the validate command is as follows:

db.runCommand({validate: "<collection_name>", options...})

<collection_name> is the name of the collection to be validated.

Options

Example

Validate a collection named “products”:

db.runCommand({ validate: 'products' });

Validate the collection and perform a background and full check:

db.runCommand({ validate: 'products', background: true, full: true });

Output

The validate command returns an object that contains information about the validation process and its results.

{
    "ns": <string>, // Namespace of the validated collection
    "nIndexes": <number>, // Number of indexes in the collection
    "keysPerIndex": {
        <index_name>: <number> // Number of keys per index
    },
    "valid": <boolean>, // If true, the collection is valid
    "errors": [<string>, ...], // Array of error messages, if any
    "warnings": [<string>, ...], // Array of warning messages, if any
    "ok": <number> // If 1, the validation command executed successfully
}

Keep in mind that the validate command should be used mainly for diagnostics and troubleshooting purposes, as it can impact system performance when validating large collections or when using the full flag. Use it when you suspect that there might be corruption or discrepancies within the collection’s data or internal structures.

That’s all about the validate command. Now you know how to check the correctness of your MongoDB collections and gather important statistics about their internal structures.