选择处理无效文档的方式
您可以指定 MongoDB 如何处理违反验证规则的文档。当操作会导致无效文档时,MongoDB 可以选择
拒绝任何违反验证标准的插入或更新操作。这是默认行为。
允许操作继续,但将在 MongoDB 日志中记录违规行为。
拒绝无效文档确保您的模式保持一致。然而,在特定场景中,您可能希望允许无效文档,例如包含在建立模式之前文档的数据迁移。
上下文
您的模式validationAction
选项决定 MongoDB 如何处理无效文档
验证行为 | 行为 |
---|---|
错误 | (默认) MongoDB 拒绝任何违反验证标准的插入或更新操作。 |
警告 | MongoDB 允许操作继续,但将在 MongoDB 日志中记录违规行为。 |
选项 1:拒绝无效文档
以下过程展示了如何创建一个拒绝无效文档的模式验证。
1
创建一个具有 validationAction: "error"
的集合。
创建一个带有 JSON 模式验证器的 contacts
集合,其具有 validationAction: "error"
db.createCollection( "contacts", { validator: { $jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb\\.com$", description: "must be a string and end with '@mongodb.com'" } } } }, validationAction: "error" } )
“错误” validationAction
导致 MongoDB 拒绝任何无效文档,并防止它们被插入到集合中。
2
尝试插入一个无效的文档。
尝试插入以下文档
db.contacts.insertOne( { name: "Amanda", email: "amanda@xyz.com" } )
该文档违反了验证规则,因为
“电子邮件”字段不匹配正则表达式模式。电子邮件字段必须以
@mongodb.com
结尾。缺少所需的
电话
字段。
操作失败,出现以下错误
MongoServerError: Document failed validation Additional information: { failingDocumentId: ObjectId("6377cca4aac957f2b77ea955"), details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'email', description: "must be a string and end with '@mongodb.com'", details: [ { operatorName: 'pattern', specifiedAs: { pattern: '@mongodb\\.com$' }, reason: 'regular expression did not match', consideredValue: 'amanda@xyz.com' } ] } ] }, { operatorName: 'required', specifiedAs: { required: [ 'phone' ] }, missingProperties: [ 'phone' ] } ] } }
选项2:允许无效文档,但在日志中记录它们
以下过程展示了如何创建一个允许无效文档但将在MongoDB日志中记录无效文档的模式验证。
1
使用validationAction: "warn"
创建一个集合。
创建一个具有validationAction: "warn"
的JSON模式验证器的contacts2
集合。
db.createCollection( "contacts2", { validator: { $jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb\\.com$", description: "must be a string and end with '@mongodb.com'" } } } }, validationAction: "warn" } )
warn
的validationAction
允许无效文档插入到集合中。无效文档将记录在MongoDB日志中。
2
3
检查日志以查找无效文档。
要以可读的格式查看MongoDB日志,请运行以下命令
db.adminCommand( { getLog:'global'} ).log.forEach(x => { print(x) } )
MongoDB日志包含类似于以下对象的条目
{ "t": { "$date": "2022-11-18T13:30:43.607-05:00" }, "s": "W", "c": "STORAGE", "id": 20294, "ctx": "conn2", "msg": "Document would fail validation", "attr": { "namespace": "test.contacts2", "document": { "_id": { "$oid": "6377cf53d59841355cac1cd0" }, "name": "Amanda", "email": "amanda@xyz.com" }, "errInfo": { "failingDocumentId": { "$oid": "6377cf53d59841355cac1cd0" }, "details": { "operatorName": "$jsonSchema", "schemaRulesNotSatisfied": [{ "operatorName": "properties", "propertiesNotSatisfied": [{ "propertyName": "email", "description": "must be a string and end with '@mongodb.com'", "details": [{ "operatorName": "pattern", "specifiedAs": { "pattern": "@mongodb\\.com$" }, "reason": "regular expression did not match", "consideredValue": "amanda@xyz.com" }] }] }, { "operatorName": "required", "specifiedAs": { "required": ["phone"] }, "missingProperties": ["phone"] }] } } } }