Docs Home → Develop Applications → MongoDB Manual
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.在这种情况下,旧文档可能无法满足新的验证要求。
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生成有效文档
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 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
提供了此操作。
The following example creates a collection with schema validation, and then inserts an invalid document by bypassing the 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" } } } } } )
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 thebypassDocumentValidation
option totrue
: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' } } ]Learn More