What this thread does
Do first: use collMod to install an overlap validator with validationAction: "warn", then inspect old and new writer payloads before enabling rejection.
Short answer
As of November 2026, this recipe follows current MongoDB docs; a dated release baseline is 8.2.12, July 22, 2026. Expand the accepted shapes before deploying new writers.
Choose / don't choose
- warn: choose to observe incompatible writes; don't treat it as enforcement because invalid writes succeed (actions).
- moderate: choose when invalid legacy documents need gradual repair while validation rules apply to new inserts and updates to valid documents; don't expect it to police updates to already-invalid records (levels).
- strict + error: choose after cleanup and compatible deployment; don't require only the new shape while an old writer still needs the old shape.
Not for: choosing MongoDB versus SQL; see postgres-vs-mongodb-adr-with-reasons-and-revisit-triggers.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
validationLevel | moderate for staged cleanup; default strict validates all inserts and updates | Current MongoDB docs, November 2026 | levels |
validationAction | warn to accept/log; error to reject (default) | Current MongoDB docs, November 2026 | actions |
anyOf | Array of JSON Schema objects; field must match at least one specified schema | Current MongoDB docs, November 2026 | keywords |
required | Field-name array alongside properties | Current MongoDB docs, November 2026 | JSON schema |
collMod / validator | Collection name and validation rules document, plus explicit level/action | Current MongoDB docs, November 2026 | command |
For an illustrative renamed field, accept a branch requiring name as a string or a branch requiring displayName as a string. These are application example names. anyOf permits documents matching both; put shared invariants outside the branches and version-specific requirements inside them. A database validator does not make an old reader understand new-only data: maintain compatible reads/writes during overlap.
Order of operations
1. Inventory actual stored shapes and writer/read compatibility. Save the current validator and an application rollback plan. 2. Expand validation to both supported shapes before new application deployment. Start warn with a validation level that observes the intended writes; moderate exempts already-invalid updates, so strict+warn is more useful when observing every write. 3. Deploy readers able to read both shapes, then compatible writers. Move to moderate+error if legacy exceptions must remain editable. Use idempotent explicit backfills and track completion; neither collMod nor warn rewrites documents. 4. Reconcile remaining old shapes with a schema query, not merely a collection health result. Retire old writers and their rollback window, migrate remaining documents, then contract the validator and select strict+error. Stennie's November 21, 2021 explanation distinguishes application migrations from server upgrades.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Document failed validation | The submitted result violates a rule | Inspect failingDocumentId, operatorName, schemaRulesNotSatisfied, propertyName, missingProperties, specifiedAs, and consideredValue; compare the failed branch with its writer's shape, then repair payload or overlap schema | error example |
Detected one or more documents not compliant with the collection's schema. See logs. | Health output can report collection validity separately from schema compliance | Query the document schema directly and count legacy exceptions before tightening | Joshua Bell, May 26, 2022 |
Restrict diagnostic access and avoid copying sensitive payloads into application logs. The error output is human-facing, not a stable parser interface (levels).
Missing
- The linked source does not confirm: Error details can expose values.
- Actual schema branches, migration transforms and old-reader compatibility are application-specific. The modify-validation overview's blanket wording about invalid updates conflicts with its own moderate example; use the dedicated level table and collMod contract for that distinction.
Started from a post by @MongoDB, packaged by the editor. Original post