Visit complete MongoDB roadmap

← Back to Topics List

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

  • full: (default: false) When set to true, the validate command conducts a more thorough inspection of the collection, looking through all its extents, which are contiguous sections of the collection’s data on disk. This option should be used with caution as it may impact read and write performance.

  • background: (default: false) When set to true, the validate command runs in the background, allowing other read and write operations on the collection to proceed concurrently. This option is beneficial for large collections, as it minimizes the impact on system performance.

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.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.