Docs HomeMongoDB Compass

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

Validation Tab“验证”选项卡

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

Validation Rules验证规则

Updated in version 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会实时更新,以显示集合中通过验证的文档和失败的文档。

JSON Schema ValidationJSON架构验证

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:考虑以下验证示例:

span class="lg-highlight-punctuation">{
$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页面。

Validation using Query Operators使用查询运算符进行验证

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字段必须与regex/@mongodb\.com$/匹配,或者
  • The status field must be either Unknown or Incomplete.status字段必须是UnknownIncomplete

Validation Actions and Levels验证操作和级别

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.验证操作确定是警告但接受无效文档,还是错误并拒绝无效文档。
  • 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

See also: 另请参阅:

Limitations局限性

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. Creating and editing validation rules is not permitted.在MongoDB Compass Readonly Edition中,您只能查看验证规则。不允许创建和编辑验证规则。