Docs Home → Develop Applications → MongoDB Manual
On this page本页内容
For documents that already exist in your collection prior to adding validation, you can specify how MongoDB applies validation rules to these documents.对于在添加验证之前已经存在于集合中的文档,您可以指定MongoDB如何将验证规则应用于这些文档。
Your schema's 您的模式的validationLevel
determines the documents for which MongoDB applies validation rules:validationLevel
确定MongoDB应用验证规则的文档:
Behavior | |
---|---|
strict
| |
moderate
|
The examples on this page use a 本页上的示例将contacts
collection with these documents:contacts
集合用于以下文档:
db.contacts.insertMany([ { "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" }, { "_id": 2, "name": "Ivan", "city": "Vancouver" } ])
strict
Validationstrict
验证The following example adds a 下面的示例向strict
validation to the contacts
collection and shows the results when attempting to update invalid documents.contacts
集合添加了strict
验证,并显示了尝试更新无效文档时的结果。
strict
validation level.strict
验证级别的验证规则。Add a validator to the 使用contacts
collection with strict
validationLevel
:strict
validationLevel
将验证器添加到contacts
集合:
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" } )
Because the 因为validationLevel
is strict
, when any document is updated, MongoDB checks that document for validation.validationLevel
是strict
,所以当更新任何文档时,MongoDB都会检查该文档是否有效。
The following update commands modify both documents in the 以下更新命令修改contacts
collection such that neither of the documents are consistent with the validation rule which requires name
to be a string:contacts
集合中的两个文档,以使两个文档都不符合要求name
为字符串的验证规则:
db.contacts.updateOne( { _id: 1 }, { $set: { name: 10 } } ) db.contacts.updateOne( { _id: 2 }, { $set: { name: 20 } } )
Both update operations fail. MongoDB returns the following output for each operation:两个更新操作都失败。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
Validationmoderate
验证The following example adds a 下面的示例向moderate
validation to the contacts
collection and shows the results when attempting to update invalid documents.contacts
集合添加了moderate
验证,并显示了尝试更新无效文档时的结果。
moderate
validation level.moderate
验证级别的验证规则。Add a validator to the 将验证器添加到具有contacts
collection with moderate
validationLevel
:validationLevel
为moderate
的contacts
集合:
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" } )
Because the 因为validationLevel
is moderate
:validationLevel
是moderate
的:
If you update the document with 如果使用_id: 1
, MongoDB applies the new validation rules because the existing document meets the validation requirements._id:1
更新文档,MongoDB将应用新的验证规则,因为现有文档满足验证要求。
If you update the document with 如果使用_id: 2
, MongoDB does not apply the new validation rules because the existing document does not meet the validation requirements._id:2
更新文档,MongoDB不会应用新的验证规则,因为现有文档不符合验证要求。
The following update commands modify both documents in the 以下更新命令修改contacts
collection such that neither of the documents are consistent with the validation rule which requires name
to be a string:contacts
集合中的两个文档,以使两个文档都不符合要求name
为字符串的验证规则:
db.contacts.updateOne( { _id: 1 }, { $set: { name: 10 } } ) db.contacts.updateOne( { _id: 2 }, { $set: { name: 20 } } )
MongoDB returns the following output for each operation: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 }
The output shows that:输出显示:
The update fails for the document with 对于_id: 1
. _id
为1
的文档,更新失败。This document met the initial validation requirements, and MongoDB applies validation rules to this document.本文档满足初始验证要求,MongoDB将验证规则应用于本文档。
The update succeeds for the document with _id: 2
. _id
为2
的文档更新成功。This document did not meet the initial validation requirements, and MongoDB does not apply validation rules to this document.本文档不符合初始验证要求,MongoDB不将验证规则应用于本文档。
The error output is intended for human consumption. 错误输出旨在供人类使用。It may change in the future and should not be relied upon in scripts.它可能在将来发生变化,不应在脚本中依赖。