Docs HomeDevelop ApplicationsMongoDB Manual

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:您可以使用以下命令和方法在每个操作的基础上绕过验证:

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.内置角色dbAdminrestore提供了此操作。

Steps步骤

The following example creates a collection with schema validation, and then inserts an invalid document by bypassing the validation rules.下面的示例使用架构验证创建一个集合,然后通过绕过验证规则插入一个无效文档。

1

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"
            }
         }
      }
   }
} )
2

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' }
   }
]

Learn More

←  Query for and Modify Valid or Invalid Documents查询和修改有效或无效文档Data Modeling Concepts数据建模概念 →