Document failed validation 🍃 MongoDB

MongoDB: MongoServerError: Document failed validation (code 121)

The collection has a JSON Schema validator and the inserted/updated document doesn’t satisfy it.

Seen on: MongoDB

Meaning

MongoDB 5+ includes errInfo details listing the failing rules (missing required fields, wrong bsonType, pattern mismatch). Mongoose validation (ValidationError) is separate and runs before the server.

Common causes

  • Required field missing
  • Wrong BSON type (string vs int/date)
  • Value not matching enum/pattern
  • Validator changed but app not updated

⚡ Quick fix

  1. Read errInfo.details for the failing property
  2. Convert types before insert (new Date(), Number())
  3. Update the validator or app schema

Detailed fix by platform

Shell

  1. mongosh "$MONGODB_URI" --eval 'db.getCollectionInfos({name:"orders"})[0].options.validator'

How to diagnose

  1. Details — errInfo.details.schemaRulesNotSatisfied
  2. Types — Document field types

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.