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
绕过每个操作的验证:
applyOps
findAndModify
db.collection.findAndModify()
mapReduce
db.collection.mapReduce()
insert
update
$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另请参阅