On this page本页内容
New in version 3.2.版本3.2中的新功能。
MongoDB provides the capability to perform schema validation during updates and insertions.MongoDB提供了在更新和插入期间执行模式验证的功能。
Validation rules are on a per-collection basis.验证规则基于每个集合。
To specify validation rules when creating a new collection, use 要在创建新集合时指定验证规则,请使用db.createCollection() with the validator option.db.createCollection()配合validator选项。
To add document validation to an existing collection, use 要向现有集合添加文档验证,请使用collMod command with the validator option.collMod命令和validator选项。
MongoDB also provides the following related options:MongoDB还提供以下相关选项:
validationLevel option, which determines how strictly MongoDB applies validation rules to existing documents during an update, andvalidationLevel选项,用于确定MongoDB在更新期间对现有文档应用验证规则的严格程度,以及validationAction option, which determines whether MongoDB should error and reject documents that violate the validation rules or warn about the violations in the log but allow invalid documents.validationAction选项,该选项确定MongoDB是否应error并拒绝违反验证规则的文档,或者warn日志中的违规行为,但允许使用无效文档。New in version 3.6.版本3.6中的新功能。
Starting in version 3.6, MongoDB supports JSON Schema validation.从版本3.6开始,MongoDB支持JSON架构验证。To specify JSON Schema validation, use the 要指定JSON架构验证,请在$jsonSchema operator in your validator expression.validator表达式中使用$jsonSchema运算符。
Note
JSON Schema is the recommended means of performing schema validation.JSON架构是执行模式验证的推荐方法。
For example, the following example specifies validation rules using JSON schema:例如,以下示例使用JSON架构指定验证规则:
For more information, see 有关更多信息,请参阅$jsonSchema.$jsonSchema。
In addition to JSON Schema validation that uses the 除了使用$jsonSchema query operator, MongoDB supports validation with other query operators, with the exception of:$jsonSchema查询运算符的JSON架构验证之外,MongoDB还支持对其他查询运算符进行验证,但以下情况除外:
For example, the following example specifies validator rules using the query expression:例如,以下示例使用查询表达式指定验证程序规则:
See also另请参阅
Validation occurs during updates and inserts.在更新和插入期间进行验证。When you add validation to a collection, existing documents do not undergo validation checks until modification.向集合添加验证时,现有文档在修改之前不会进行验证检查。
The validationLevel option determines which operations MongoDB applies the validation rules:validationLevel选项确定MongoDB应用验证规则的操作:
validationLevel is strict (the default), MongoDB applies validation rules to all inserts and updates.validationLevel是strict(默认值),MongoDB将验证规则应用于所有插入和更新。validationLevel is moderate, MongoDB applies validation rules to inserts and to updates to existing documents that already fulfill the validation criteria.validationLevel为moderate,MongoDB会将验证规则应用于已满足验证标准的现有文档的插入和更新。moderate level, updates to existing documents that do not fulfill the validation criteria are not checked for validity.moderate级别,对不符合验证标准的现有文档的更新不会进行有效性检查。For example, create a 例如,使用以下文档创建contacts collection with the following documents:contacts集合:
Issue the following command to add a validator to the 发出以下命令将验证程序添加到contacts collection:contacts集合:
The contacts collection now has a validator with the moderate validationLevel:contacts集合现在有一个具有moderate校验级别的验证器:
_id of 1, MongoDB would apply the validation rules since the existing document matches the criteria._id为1的文档,MongoDB将应用验证规则,因为现有文档符合条件。_id of 2 as it does not meet the validation rules._id为2的文档的更新,因为它不符合验证规则。To disable validation entirely, you can set 要完全禁用验证,可以将validationLevel to off.validationLevel设置为off。
The validationAction option determines how MongoDB handles documents that violate the validation rules:validationAction选项确定MongoDB如何处理违反验证规则的文档:
validationAction is error (the default), MongoDB rejects any insert or update that violates the validation criteria.validationAction为error(默认值),MongoDB将拒绝任何违反验证条件的插入或更新。validationAction is warn, MongoDB logs any violations but allows the insertion or update to proceed.validationAction为warn,MongoDB将记录任何违规行为,但允许继续插入或更新。For example, create a 例如,使用以下JSON架构验证程序创建contacts2 collection with the following JSON Schema validator:contacts2集合:
With the 使用warn validationAction, MongoDB logs any violations but allows the insertion or update to proceed.warn validationAction,MongoDB会记录任何违规行为,但允许继续插入或更新。
For example, the following insert operation violates the validation rule:例如,以下插入操作违反了验证规则:
However, since the 但是,由于validationAction is warn only, MongoDB only logs the validation violation message and allows the operation to proceed:validationAction是只是warn,MongoDB只记录验证冲突消息并允许操作继续:
You cannot specify a validator for collections in the 不能为admin, local, and config databases.admin、local和config数据库中的集合指定验证程序。
You cannot specify a validator for 不能为system.* collections.system.*集合指定验证程序。
Users can bypass document validation using the 用户可以使用bypassDocumentValidation option.bypassDocumentValidation选项绕过文档验证。
The following commands can bypass validation per operation using the new option 以下命令可以使用新选项bypassDocumentValidation:bypassDocumentValidation绕过每个操作的验证:
applyOpsfindAndModifydb.collection.findAndModify()mapReducedb.collection.mapReduce()insertupdate$out and $merge stages for the aggregate command and db.collection.aggregate() methodaggregate命令和db.collection.aggregate()方法的$out和$merge阶段For deployments that have enabled access control, to bypass document validation, the authenticated user must have 对于启用了访问控制的部署,要绕过文档验证,经过身份验证的用户必须具有bypassDocumentValidation action.bypassDocumentValidation操作。The built-in roles 内置角色dbAdmin and restore provide this action.dbAdmin和restore提供此操作。
See also另请参阅