0PricingLogin
MongoDB Academy · Lesson

Validation Levels and Actions

Learners will configure validationLevel (strict vs moderate) and validationAction (error vs warn) to control how violations are handled.

Controlling How Strict Validation Is

MongoDB gives you two independent knobs to tune schema validation behaviour: validationLevel controls which documents are subject to validation, while validationAction controls what happens when a document fails validation. Together they let you introduce validation gradually into existing collections without breaking current data or applications.

validationLevel: strict

strict is the default validation level. In strict mode, every insert and every update must pass the validator—no exceptions. If an existing document in the collection already violates the schema, attempting to update it will still be checked against the validator. Strict mode gives you the strongest data quality guarantee but can be disruptive when applied to a collection with legacy non-conforming documents.

db.runCommand({
  collMod: 'users',
  validator: { $jsonSchema: { /* ... */ } },
  validationLevel: 'strict'   // default — all inserts and updates must pass
});

All lessons in this course

  1. Adding a Validator to a Collection
  2. Type, Required, and Enum Constraints
  3. Validation Levels and Actions
  4. Evolving Schemas Without Downtime
← Back to MongoDB Academy