mongodb
data_error
ai_generated
true
MongoServerError:文档验证失败:不允许额外属性:'extraField'
MongoServerError: Document failed validation: Additional properties not allowed: 'extraField'
ID: mongodb/schema-validation-type-error
90%修复率
85%置信度
1证据数
2023-11-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 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 | — | — | — |
根因分析
插入或更新的文档违反了集合的 JSON Schema 验证器,该验证器将 additionalProperties 设置为 false。
English
A document being inserted or updated violates the collection's JSON Schema validator that has additionalProperties set to false.
官方文档
https://www.mongodb.com/docs/manual/core/schema-validation/解决方案
-
Modify the document to remove the extra field before insertion. Example in Node.js: delete doc.extraField; await collection.insertOne(doc);
-
Update the collection validator to allow the extra field: db.runCommand({ collMod: 'mycollection', validator: { $jsonSchema: { bsonType: 'object', additionalProperties: true, properties: { extraField: { bsonType: 'string' } } } } }) -
Use validationAction: 'warn' instead of 'error' to log violations without blocking writes: db.runCommand({ collMod: 'mycollection', validationAction: 'warn' })
无效尝试
常见但无效的做法:
-
100% 失败
This removes all data and validation, which is destructive and not a fix for the schema mismatch.
-
70% 失败
This bypasses validation entirely, defeating the purpose of schema enforcement.
-
100% 失败
The document will always fail validation unless it is modified.