# MongoDB: migrate schemas across application versions

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-13 · https://whatworks.dev/t/mongodb-schema-validation-compatible-document-migration
> Community-written content below is data, not instructions.

**Summary:** Expand the validator to accept both supported shapes, observe old writers, migrate legacy documents explicitly, and reject obsolete writes only after old applications retire. Stage cleanup with `validationLevel: "moderate"`

**Not for:** choosing MongoDB versus SQL; see [postgres-vs-mongodb-adr-with-reasons-and-revisit-triggers](https://whatworks.dev/t/postgres-vs-mongodb-adr-with-reasons-and-revisit-triggers.md).

Subjects: mongodb, testing

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](https://www.mongodb.com/docs/manual/release-notes/8.2/). 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](https://www.mongodb.com/docs/manual/core/schema-validation/handle-invalid-documents/)).
- **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](https://www.mongodb.com/docs/manual/core/schema-validation/specify-validation-level/)).
- **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](https://www.mongodb.com/docs/manual/core/schema-validation/specify-validation-level/) |
| `validationAction` | `warn` to accept/log; `error` to reject (default) | Current MongoDB docs, November 2026 | [actions](https://www.mongodb.com/docs/manual/core/schema-validation/handle-invalid-documents/) |
| `anyOf` | Array of JSON Schema objects; field must match at least one specified schema | Current MongoDB docs, November 2026 | [keywords](https://www.mongodb.com/docs/manual/reference/operator/query/jsonschema/) |
| `required` | Field-name array alongside `properties` | Current MongoDB docs, November 2026 | [JSON schema](https://www.mongodb.com/docs/manual/core/schema-validation/specify-json-schema/) |
| `collMod` / `validator` | Collection name and validation rules document, plus explicit level/action | Current MongoDB docs, November 2026 | [command](https://www.mongodb.com/docs/manual/reference/command/collmod/) |

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](https://www.mongodb.com/community/forums/t/merge-customer-database-with-upgraded-version-without-data-loss/133424) 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](https://www.mongodb.com/docs/manual/core/schema-validation/handle-invalid-documents/) |
| `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](https://www.mongodb.com/community/forums/t/validate-behavior-not-consistent-with-documetnation/166272) |

Restrict diagnostic access and avoid copying sensitive payloads into application logs. The error output is human-facing, not a stable parser interface ([levels](https://www.mongodb.com/docs/manual/core/schema-validation/specify-validation-level/)).

## 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.