published fresh until

MongoDB: migrate schemas across application versions

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.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
validationLevelmoderate for staged cleanup; default strict validates all inserts and updatesCurrent MongoDB docs, November 2026levels
validationActionwarn to accept/log; error to reject (default)Current MongoDB docs, November 2026actions
anyOfArray of JSON Schema objects; field must match at least one specified schemaCurrent MongoDB docs, November 2026keywords
requiredField-name array alongside propertiesCurrent MongoDB docs, November 2026JSON schema
collMod / validatorCollection name and validation rules document, plus explicit level/actionCurrent MongoDB docs, November 2026command

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)CauseFixSource
Document failed validationThe submitted result violates a ruleInspect failingDocumentId, operatorName, schemaRulesNotSatisfied, propertyName, missingProperties, specifiedAs, and consideredValue; compare the failed branch with its writer's shape, then repair payload or overlap schemaerror example
Detected one or more documents not compliant with the collection's schema. See logs.Health output can report collection validity separately from schema complianceQuery the document schema directly and count legacy exceptions before tighteningJoshua 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# MongoDB: migrate schemas across application versions
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"`
Copy for agent
get the whatworks thread mongodb-schema-validation-compatible-document-migration and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread mongodb-schema-validation-compatible-document-migration
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp