# MongoServerError: Document failed validation: Additional properties not allowed: 'extraField'

- **ID:** `mongodb/schema-validation-type-error`
- **Domain:** mongodb
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A document being inserted or updated violates the collection's JSON Schema validator that has additionalProperties set to false.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| mongodb-3.6 | active | — | — |
| mongodb-4.0 | active | — | — |
| mongodb-4.2 | active | — | — |
| mongodb-4.4 | active | — | — |
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## Workarounds

1. **Modify the document to remove the extra field before insertion. Example in Node.js: delete doc.extraField; await collection.insertOne(doc);** (90% success)
   ```
   Modify the document to remove the extra field before insertion. Example in Node.js: delete doc.extraField; await collection.insertOne(doc);
   ```
2. **Update the collection validator to allow the extra field: db.runCommand({ collMod: 'mycollection', validator: { $jsonSchema: { bsonType: 'object', additionalProperties: true, properties: { extraField: { bsonType: 'string' } } } } })** (85% success)
   ```
   Update the collection validator to allow the extra field: db.runCommand({ collMod: 'mycollection', validator: { $jsonSchema: { bsonType: 'object', additionalProperties: true, properties: { extraField: { bsonType: 'string' } } } } })
   ```
3. **Use validationAction: 'warn' instead of 'error' to log violations without blocking writes: db.runCommand({ collMod: 'mycollection', validationAction: 'warn' })** (80% success)
   ```
   Use validationAction: 'warn' instead of 'error' to log violations without blocking writes: db.runCommand({ collMod: 'mycollection', validationAction: 'warn' })
   ```

## Dead Ends

- **** — This removes all data and validation, which is destructive and not a fix for the schema mismatch. (100% fail)
- **** — This bypasses validation entirely, defeating the purpose of schema enforcement. (70% fail)
- **** — The document will always fail validation unless it is modified. (100% fail)
