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 Rules验证规则
The validation editor supports JSON Schema validation, and validation with query expressions using Validation using Query Operators.验证编辑器支持JSON模式验证,以及使用查询运算符进行验证的查询表达式验证。
JSON Schema ValidationJSON模式验证
The $jsonSchema operator supports various keywords to specify validation rules. For example:$jsonSchema运算符支持各种键来指定验证规则。例如:
Therequiredarray defines required fields in your document.required数组定义了文档中的必填字段。Thepropertiesobject defines rules for specific document fields.properties对象定义了特定文档字段的规则。
To generate a JSON schema by analyzing existing sample data, see Generate Validation Rules.要通过分析现有示例数据生成JSON模式,请参阅生成验证规则。
Example示例
Consider the following example validation:考虑以下验证示例:
{
$jsonSchema: {
bsonType: "object",
required: [ "address", "borough", "name" ],
properties: {
address: {
bsonType: "object",
properties: {
coord: {
bsonType: "array",
items: [
{
bsonType: "double",
minimum: -180,
maximum: 180,
exclusiveMaximum: false,
description: "must be a number in [ -180, 180 ]"
},
{
bsonType: "double",
minimum: -90,
maximum: 90,
exclusiveMaximum: false,
description: "must be a number in [ -90, 90 ]"
}
]
}
},
description: "must be an object"
},
borough: {
bsonType: "string",
enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ],
description: "must be one of the enum strings"
}
}
}
}
This validation specifies:此验证指定:
The list of required fields.必填字段列表。The bsonType for all required fields.所有必填字段的bsonType。The minimum and maximum values in theaddress.coordarray.address.coord数组中的最小值和最大值。The acceptable values for the使用枚举表示boroughfield, using enum.borough字段的可接受值。
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: [
{ name: { $type: "string" } },
{ borough: {
bsonType: "string",
enum: [ "Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island" ],
description: "must be one of the enum strings"
} }
]
}
Using this validation, one of the following must be true:使用此验证时,必须满足以下条件之一:
Thenamefield must be BSON type string.name字段必须是BSON类型的字符串。Theboroughfield must be one of the enum strings.borough字段必须是枚举字符串之一。
Validation Actions and Levels验证行动和级别
At the top, specify a Validation Action and Validation Level:在顶部,指定“验证操作”和“验证级别”:
The validation action determines whether to验证操作决定是警告但接受无效文档,还是错误并拒绝无效文档。warnbut accept invalid documents, orerrorand reject invalid documents.The validation level determines how strictly MongoDB applies validation rules to existing documents.验证级别决定了MongoDB对现有文档应用验证规则的严格程度。Strictvalidation applies your rules to all document inserts and updates.验证将规则应用于所有文档插入和更新。Moderatevalidation 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手册中的指定验证规则。
Get Started开始使用
Limitations局限性
The Validation tab is not available if you are connected to Atlas Data Federation.如果您连接到Atlas Data Federation,则“验证”选项卡不可用。
Compass cannot validate a schema that has more than 1000 distinct fields. If you try to validate a schema with more than 1000 distinct fields, Compass returns an error.Compass无法验证具有1000多个不同字段的架构。如果您尝试验证具有1000多个不同字段的模式,Compass将返回错误。
In MongoDB Compass Readonly Edition, you can only view validation rules. Creating and editing validation rules is not permitted.在MongoDB Compass只读版中,您只能查看验证规则。不允许创建和编辑验证规则。