Set Validation Rules for Your Schema为您的架构设置验证规则

The Validation tab allows you to manage schema validation rules for a collection.“验证”选项卡允许您管理集合的架构验证规则

Schema validation ensures that all documents in a collection follow a defined set of rules, such as conforming to a specific shape or only allowing a specified range of values in fields.架构验证确保集合中的所有文档都遵循一组定义的规则,例如符合特定形状或仅允许字段中指定的值范围。

Validation view

Updated in version 1.18.在版本1.18中更新。

The validation editor supports JSON Schema validation, and validation with query expressions using query operators. 验证编辑器支持JSON架构验证,以及使用查询运算符对查询表达式进行验证。As you edit your validation, Compass updates in real-time to display a document from your collection that passes the validation and a document that fails.编辑验证时,Compass会实时更新,以显示通过验证的集合中的文档和失败的文档。

To specify JSON Schema validation, use the $jsonSchema operator.要指定JSON架构验证,请使用$jsonSchema运算符。

{
   $jsonSchema: {
      required: ['customer'], // the customer field is required
      properties: {
      purchaseMethod: {
         enum: ['In Store','Online'],
         description: "can only be either 'In Store' or 'Online'"
      }
      }
   }
}

The $jsonSchema operator supports various keywords to specify validation rules. $jsonSchema运算符支持各种关键字来指定验证规则。For example:例如:

  • The required array defines required fields in your document.required数组定义文档中的必填字段。
  • The properties object defines rules for specific document fields.properties对象定义特定文档字段的规则。

Consider the following example validation:考虑下面的示例验证:

{
   $jsonSchema: {
      bsonType: "object",
      required: [ "name", "year", "major", "gpa", "address.city", "address.street" ],
      properties: {
         name: {
            bsonType: "string",
            description: "must be a string"
         },
         year: {
            bsonType: "int",
            minimum: 2017,
            maximum: 3017,
            exclusiveMaximum: false,
            description: "must be an integer in [ 2017, 3017 ]"
         },
         major: {
            bsonType: "string",
            enum: [ "Math", "English", "Computer Science", "History", null ],
            description: "can only be one of the enum values"
         },
         gpa: {
            bsonType: [ "double" ],
            minimum: 0,
            description: "must be a double"
         }
      }
   }
}

This validation specifies:此验证指定:

For all available $jsonSchema keywords, refer to the $jsonSchema page in the MongoDB manual.有关所有可用的$jsonSchema关键字,请参阅MongoDB手册中的$jsonSchema页面。

You can also specify validation using query operators, with the exception of the following query operators: $near, $nearSphere, $text, and $where.您还可以使用查询运算符指定验证,但以下查询运算符除外:$near$nearSphere$text$where

{
   $or: [
      { phone: { $type: "string" } },
      { email: { $regex: /@mongodb\.com$/ } },
      { status: { $in: [ "Unknown", "Incomplete" ] } }
   ]
}

Using this validation, one of the following must be true:使用此验证时,必须满足以下条件之一:

  • The phone field must be BSON type string,phone字段必须是BSON类型字符串,
  • The email field must match the regex /@mongodb\.com$/, oremail字段必须与正则表达式/@mongodb\.com$/匹配,或
  • The status field must be either Unknown or Incomplete.status字段必须为UnknownIncomplete

At the top, specify a Validation Action and Validation Level:在顶部,指定“验证操作”和“验证级别”:

  • The validation action determines whether to warn but accept invalid documents, or error and reject invalid documents.验证操作确定是warn但接受无效文档,还是error并拒绝无效文档。
  • The validation level determines how strictly MongoDB applies validation rules to existing documents.验证级别决定MongoDB对现有文档应用验证规则的严格程度。

    • Strict validation applies your rules to all document inserts and updates.验证将规则应用于所有文档插入和更新。
    • Moderate validation only applies your rules to new documents and existing valid documents. 验证仅将规则应用于新文档和现有有效文档。Existing invalid documents are not affected.现有的无效文档不受影响。

For details on validation actions and levels, see Specify Validation Rules in the MongoDB manual.有关验证操作和级别的详细信息,请参阅MongoDB手册中的指定验证规则

Tip

The Validation tab is not available if you are connected to a Data Lake.如果连接到数据湖,“验证”选项卡不可用。

In MongoDB Compass Readonly Edition, you can only view validation rules. MongoDB Compass只读版中,只能查看验证规则。Creating and editing validation rules is not permitted.不允许创建和编辑验证规则。