指定现有文档的验证级别
对于在添加验证之前集合中已经存在的文档,您可以指定 MongoDB 如何将验证规则应用于这些文档。
语境
您的模式validationLevel确定 MongoDB 应用验证规则的文档:
| 验证级别 | 行为 | 
|---|---|
| strict | (默认)MongoDB 将验证规则应用于所有插入和更新。 | 
| moderate | MongoDB 仅将验证规则应用于现有的有效文档。不检查在添加验证之前存在的对无效文档的更新的有效性。 | 
先决条件
此页面上的示例使用contacts包含这些文档的集合:
db.contacts.insertMany([
   { "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
   { "_id": 2, "name": "Ivan", "city": "Vancouver" }
])
步骤:使用strict验证
以下示例strict向集合添加验证contacts 并显示尝试更新无效文档时的结果。
- 指定具有 - strict验证级别的验证规则。- 将验证器添加到 - contacts集合中- strict- validationLevel:- db.runCommand( { collMod: "contacts", validator: { $jsonSchema: { bsonType: "object", required: [ "phone", "name" ], properties: { phone: { bsonType: "string", description: "phone must be a string and is required" }, name: { bsonType: "string", description: "name must be a string and is required" } } } }, validationLevel: "strict" } )- 因为 - validationLevel是- strict,当任何文档更新时,MongoDB 会检查该文档以进行验证。
- 测试验证。 - 以下更新命令修改 - contacts集合中的两个文档,使得两个文档都不符合要求- name为字符串的验证规则:- db.contacts.updateOne( { _id: 1 }, { $set: { name: 10 } } ) db.contacts.updateOne( { _id: 2 }, { $set: { name: 20 } } )
- 观察结果。 - 两次更新操作均失败。MongoDB 为每个操作返回以下输出: - MongoServerError: Document failed validation Additional information: { failingDocumentId: <id>, details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'name', description: 'name must be a string and is required', details: [ { operatorName: 'bsonType', specifiedAs: { bsonType: 'string' }, reason: 'type did not match', consideredValue: <value>, consideredType: 'int' } ] } ] }, { operatorName: 'required', specifiedAs: { required: [ 'phone', 'name' ] }, missingProperties: [ 'phone' ] } ] } }
步骤:使用moderate验证
以下示例moderate向集合添加验证contacts 并显示尝试更新无效文档时的结果。
- 指定具有 - moderate验证级别的验证规则。- 将验证器添加到 - contacts集合中- moderate- validationLevel:- db.runCommand( { collMod: "contacts", validator: { $jsonSchema: { bsonType: "object", required: [ "phone", "name" ], properties: { phone: { bsonType: "string", description: "phone must be a string and is required" }, name: { bsonType: "string", description: "name must be a string and is required" } } } }, validationLevel: "moderate" } )- 因为 - validationLevel是- moderate:- 如果使用 更新文档_id: 1,MongoDB 将应用新的验证规则,因为现有文档满足验证要求。
- 如果使用 更新文档_id: 2,MongoDB 不会应用新的验证规则,因为现有文档不符合验证要求。
 
- 如果使用 更新文档
- 测试验证。 - 以下更新命令修改 - contacts集合中的两个文档,使得两个文档都不符合要求- name为字符串的验证规则:- db.contacts.updateOne( { _id: 1 }, { $set: { name: 10 } } ) db.contacts.updateOne( { _id: 2 }, { $set: { name: 20 } } )
- 观察结果。 - MongoDB 为每个操作返回以下输出: - // _id: 1 MongoServerError: Document failed validation Additional information: { failingDocumentId: 1, details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'name', description: 'name must be a string and is required', details: [ { operatorName: 'bsonType', specifiedAs: { bsonType: 'string' }, reason: 'type did not match', consideredValue: 10, consideredType: 'int' } ] } ] } ] } } // _id: 2 { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 0, upsertedCount: 0 }- 输出显示: - 带有 的文档更新失败_id: 1。该文档满足初始验证要求,MongoDB 对该文档应用验证规则。
- 带有 的文档更新成功_id: 2。该文档不满足初始验证要求,MongoDB 不对该文档应用验证规则。
 
- 带有 的文档更新失败
[IMPORTANT]
错误输出供人类使用。它将来可能会发生变化,不应在脚本中依赖它。
学到更多
参见
原文:Specify Validation Level for Existing Documents
译者:景圣