For documents that already exist in your collection before you add validation, you can specify how MongoDB applies validation rules to these documents.
Context
Your schema's validationLevel determines the documents for which MongoDB applies validation rules:
Validation Level | Behavior |
|---|---|
| MongoDB applies the same validation rules to document inserts and updates to existing valid documents that match the validation rules. Updates to existing documents in the collection that don't match the validation rules aren't required to pass validation. |
| (Default) MongoDB applies the same validation rules to all document inserts and updates. |
| MongoDB applies the validation rules to all document inserts and updates, and guarantees that every document in the collection satisfies the rules. Unlike
New in version 9.0. : |
Prerequisite
The examples on this page use a movies collection. To create the collection and insert the documents that the examples use, run the following command:
db.movies.insertMany( [ { title: "The Godfather", year: 1972 }, { title: "Casablanca", year: 1942 } ] )
Specify moderate Validation
The following example adds a moderate validator to the movies collection and inserts a document that satisfies the validation rules.
Specify validation rules with moderate validation level.
Add a validator to the movies collection with moderate validationLevel:
db.runCommand( { collMod: "movies", validator: { $jsonSchema: { bsonType: "object", required: [ "title" ], properties: { title: { bsonType: "string", description: "title must be a string and is required" } } } }, validationLevel: "moderate" } )
Because the validationLevel is moderate, MongoDB applies the validation rules to all document inserts and to updates of existing documents that already meet the rules.
Specify strict Validation
The following example adds a strict validator to the movies collection and inserts a document that satisfies the validation rules.
Specify validation rules with strict validation level.
Add a validator to the movies collection with strict validationLevel:
db.runCommand( { collMod: "movies", validator: { $jsonSchema: { bsonType: "object", required: [ "title" ], properties: { title: { bsonType: "string", description: "title must be a string and is required" } } } }, validationLevel: "strict" } )
Because the validationLevel is strict, MongoDB applies the validation rules to all document inserts and updates.
Note
When you upgrade to strict validation, MongoDB does not retroactively check that existing documents in the collection pass the validator. After the upgrade, the collection may contain invalid documents that existed before the upgrade. However, update operations on invalid documents fail because of the validator. To update an invalid document, first resolve the issue that fails the validator.
Specify constraint Validation
The following example upgrades the movies collection to the constraint validation level. When you upgrade to the constraint level, MongoDB checks that every document already in the collection satisfies the validator.
Note
The following procedure applies to a collection that already exists. If you set the validationLevel to constraint when you create the collection, you don't need to set the level to strict first or prepare the collection.
(Conditional) Set your validationLevel to strict.
If your collection's validationLevel is not already strict, follow the procedure to specify strict validation.
Prepare the collection for the constraint level.
Run collMod with prepareConstraintValidationLevel set to true:
db.runCommand( { collMod: "movies", prepareConstraintValidationLevel: true } )
This command prepares the collection for the upgrade by blocking bypassDocumentValidation and preventing changes to the validator.
Set the validation level to constraint.
Run collMod to set the validationLevel to constraint. The validationAction must be error or errorAndLog:
db.runCommand( { collMod: "movies", validationLevel: "constraint", validationAction: "error" } )
You can't change the validator in the same collMod operation that upgrades the validationLevel to constraint. Set the validation rules before you run this command. To change the rules later, downgrade the level to strict, modify the rules, and then upgrade back to constraint.
Because the validationLevel is constraint, MongoDB applies the validation rules to all document inserts and updates, regardless of whether an existing document met the rules before the upgrade.
When you set the level to constraint, MongoDB scans all existing documents in the collection and fails the collMod operation if any document violates the validator. This scan can be computationally expensive for large collections. If the operation fails, update the violating documents to pass the validator before you continue.
Important
The error output is intended for human consumption. It may change in the future and should not be relied upon in scripts.