Bypass Schema Validation绕过架构验证
On this page本页内容
In some situations, you may need to bypass a collection's schema validation rules. 在某些情况下,您可能需要绕过集合的架构验证规则。For example, if you are restoring potentially invalid data from a backup to a collection that has validation rules. 例如,如果要将备份中可能无效的数据恢复到具有验证规则的集合中。In this scenario, older documents may not meet new validation requirements.在这种情况下,旧文档可能无法满足新的验证要求。
Context上下文
Bypassing schema validation is done on a per-operation basis. 绕过模式验证是在每个操作的基础上完成的。If you bypass schema validation to insert an invalid document, any future updates to the invalid document must either:如果绕过架构验证来插入无效文档,则将来对无效文档的任何更新都必须:
Also bypass schema validation还绕过架构验证Result in a valid document生成有效文档
Supported Operations支持的操作
You can use the following commands and methods to bypass validation on a per-operation basis:您可以使用以下命令和方法在每个操作的基础上绕过验证:
applyOps
command命令findAndModify
command and命令和db.collection.findAndModify()
method方法mapReduce
command and命令和db.collection.mapReduce()
method方法insert
command命令update
command命令$out
and$merge
stages for theaggregate
command anddb.collection.aggregate()
methodaggregate
命令和db.collection.aggregate()
方法的$out
和$merge
阶段
Prerequisite先决条件
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
提供了此操作。
Steps步骤
The following example creates a collection with schema validation, and then inserts an invalid document by bypassing the validation rules.以下示例创建了一个具有架构验证的集合,然后绕过验证规则插入一个无效文档。
Create a collection with validation rules使用验证规则创建集合
Create a 创建students
collection and use the $jsonSchema
operator to set schema validation rules:students
集合并使用$jsonSchema
运算符设置架构验证规则:
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"
}
}
}
}
} )
Bypass the validation to insert an invalid document绕过验证以插入无效文档
The following document is invalid because the 以下文档无效,因为year
field is outside of the allowed bounds (2017
-3017
):year
字段超出了允许的范围(2017
-3017
):
{
name: "Alice",
year: Int32( 2016 ),
major: "History",
gpa: Double(3.0),
address: {
city: "NYC",
street: "33rd Street"
}
}
To bypass the validation rules and insert the invalid document, run the following 要绕过验证规则并插入无效文档,请运行以下insert
command, which sets the bypassDocumentValidation
option to true
:insert
命令,该命令将bypassDocumentValidation
选项设置为true
:
db.runCommand( {
insert: "students",
documents: [
{
name: "Alice",
year: Int32( 2016 ),
major: "History",
gpa: Double(3.0),
address: {
city: "NYC",
street: "33rd Street"
}
}
],
bypassDocumentValidation: true
} )
Results结果
To confirm that the document was successfully inserted, query the 若要确认文档已成功插入,请查询students
collection:students
集合:
db.students.find()
MongoDB returns the inserted document:MongoDB返回插入的文档:
[
{
_id: ObjectId("62bcb4db3f7991ea4fc6830e"),
name: 'Alice',
year: 2016,
major: 'History',
gpa: 3,
address: { city: 'NYC', street: '33rd Street' }
}
]