MongoDB / Editorial solution guide

Diagnose document validation, type and unique-key failures

Separate error 121 validation from error 11000 uniqueness and investigate BSON values before coercion or bypass. Preserve rejected business records for explicit repair.

About 4 min readContent reviewed

Read-only knowledge base ยท No public comments

Recognize the symptoms

A write returns Document failed validation (121), E11000 duplicate key error, or an apparently valid numeric/date value is rejected. A query may also quietly miss text-form identifiers or amounts. The visible JSON representation can hide BSON distinctions, and a successful API-side check does not prove the collection validator or unique-index contract is satisfied.

Understand the likely causes

  • Validation can fail on missing required fields, wrong BSON types, ranges or the proposed post-update shape. Uniqueness can fail on existing indexed values, collation equivalence or optional null/missing keys. A partial index constrains only its declared subset. Mixed schema versions and driver serialization can create both visible failures and silent report omissions.

Preserve the structured failure and rejected intent

Keep the error code, named index or rule details, operation type and request identifier, with sensitive values redacted. Code 121 and code 11000 need different remedies; do not catch both as harmless duplicates. Inspect what the complete proposed document would look like after an update or replacement. Replacing a document can remove required fields that were not included, even when the original stored version was valid. Preserve the rejected business event so repair does not silently discard legitimate work.

Read the active collection and index contracts

An existing authorized training session can inspect collection options and indexes. Check validator, validationLevel and validationAction together with unique, partial, sparse and collation properties. These definitions describe rules, not proof that all old documents are valid. Adding a validator does not retroactively convert historical data. The methods below are read-only illustrations; no real definitions are changed and no production metadata is fetched during authoring. A missing privilege should be handled through the normal review process rather than broadening application access.

javascript Illustrative mongosh; results are reasoned, not measured.
db.getSiblingDB('quay_lab').getCollectionInfos({ name: 'inventory' });
db.getSiblingDB('quay_lab').inventory.getIndexes();

Actual validators and index definitions depend on the prepared fixture and inspection privileges.

Inspect actual BSON types, missing and null

Group the affected population by type and schema version before deciding how to repair it. The aggregation expression $type can label missing and null separately, unlike an equality-to-null filter that can match both. A validator requiring bsonType int need not accept a double or string that prints like the same quantity. Date strings likewise are not BSON dates. Define accepted conversions, source time zones and exact numeric ranges, and quarantine invalid values instead of converting every failure to zero or empty text.

javascript Illustrative mongosh; results are reasoned, not measured.
db.getSiblingDB('quay_lab').inventory.aggregate([
  { $group: { _id: { version: '$schemaVersion', qtyType: { $type: '$qty' } }, count: { $sum: 1 } } },
  { $sort: { '_id.version': 1, '_id.qtyType': 1 } }
]);

The inventory observes stored types only. It performs no migration and claims no pre-existing distribution.

Repair the correct rule without bypassing it

For validation failure, correct the incoming shape or stage a reviewed compatible schema migration. For duplicate keys, identify whether the request is an idempotent retry or a genuinely conflicting record; return a recorded result only when event identity proves it. Optional fields may need a deliberately designed partial unique index, not deletion of arbitrary duplicates. A unique multikey index does not necessarily forbid repeated elements inside one document, and sharded uniqueness has routing-key restrictions. Review those contexts before changing constraints or inventing cross-document guarantees.

Verify the fix

On an isolated copy, test accepted, missing, null, wrong-type, out-of-range and duplicate/collation-equivalent inputs. Verify corrected records retain their business identity and required fields. Re-run type/version inventories and representative reports to confirm totals no longer omit malformed values. Test both rejection and permitted writes under the final rule, including old-reader compatibility during migration.

Keep it from recurring

  • Keep API, driver BSON and collection rules aligned; preserve structured errors and rejected-event evidence. Document optional-field uniqueness, schemaVersion transitions and conversion policy. Validate existing collisions before index rollout and avoid granting routine validation bypass to application roles merely to make imports appear successful.

Official references