模式验证
在本页中
版本3.2中的新功能
MongoDB提供了在更新和插入期间执行模式验证的功能。
验证规则基于每个集合。
MongoDB还提供了以下相关选项:
validationLevel
选项,该选项确定MongoDB在更新期间对现有文档应用验证规则的严格程度,以及validationAction
选项,该选项确定MongoDB是否应显示错误并拒绝违反验证规则的文档,或warn
日志中的违反行为,但允许无效文档。
版本3.6中的新功能
注意推荐使用JSON模式执行模式验证。
例如,以下示例使用JSON模式指定验证规则:
db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
"description": "must be a string and is required"
}
}
}
}
}
}
})
除了使用
$jsonSchema
操作查询运算符,MongoDB支持使用 其他查询运算符 查询选择器,除了$near
,[$nearSphere](https://docs.mongodb.com/manual/reference/operator/query/nearSphere/'35;操作/u nearSphere), $text
,和 $where
运算符。例如,以下示例使用查询表达式指定验证器规则:
db.createCollection( "contacts",
{ validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )
另请参见
在更新和插入期间进行验证。将验证添加到集合时,现有文档在修改之前不会进行验证检查。
validationLevel
选项确定MongoDB应用验证规则的操作:- 如果
validationLevel
是strict
(默认值),MongoDB会对所有插入和更新应用验证规则。 - 如果
validationLevel
是moderate
,MongoDB将验证规则应用于已满足验证条件的现有文档的插入和更新。使用moderate
级别时,不检查对不符合验证条件的现有文档的更新是否有效。
例如,使用以下文档创建
contacts
集合:db.contacts.insert([
{ "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
{ "_id": 2, "name": "Ivan", "city": "Vancouver" }
])
发出以下命令将验证器添加到
contacts
集合:db.runCommand( {
collMod: "contacts",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
name: {
bsonType: "string",
description: "must be a string and is required"
}
}
} },
validationLevel: "moderate"
} )
这
contacts
集合现在有一个使用 moderate
验证级别的验证器:- 如果试图更新
_id
为1, MongoDB将应用验证规则,因为现有文档与条件匹配。 - 相反,MongoDB不会对
_id
为2的文档应用验证规则,因为它不符合验证规则。
要完全禁用验证,可以将
validationLevel
设置为off
。validationAction
选项确定MongoDB如何处理违反验证规则的文档:- 如果
validationAction
为error
(默认值),MongoDB将拒绝任何违反验证条件的插入或更新。 - 如果
validationAction
为warn
,MongoDB会记录任何冲突,但允许继续插入或更新。
例如,使用以下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 match the regular expression pattern"
},
status: {
enum: [ "Unknown", "Incomplete" ],
description: "can only be one of the enum values"
}
}
} },
validationAction: "warn"
} )
例如,以下插入操作违反了验证规则:
db.contacts2.insert( { name: "Amanda", status: "Updated" } )
不过,由于
validationAction
仅为warn
,MongoDB只记录验证冲突消息并允许操作继续:2017-12-01T12:31:23.738-0500 W STORAGE [conn1] Document would fail validation collection: example.contacts2 doc: { _id: ObjectId('5a2191ebacbbfc2bdc4dcffc'), name: "Amanda", status: "Updated" }
不能为
admin
、local
和config
数据库中的集合指定验证器。不能为
system.*
集合指定验证器。用户可以使用
bypassDocumentValidation
选项绕过文档验证。以下命令可以使用新选项
bypassDocumentValidation
跳过每个操作的验证:另请参见
译者:张鹏