文档菜单
文档首页
/
MongoDB 手册
/ /

指定现有文档的验证级别

本页内容

  • 上下文
  • 先决条件
  • 步骤:使用严格验证
  • 步骤:使用 适度验证
  • 了解更多

对于在添加验证之前已存在于您的集合中的文档,您可以指定MongoDB如何将这些验证规则应用于这些文档。

您的模式validationLevel决定MongoDB应用验证规则的对象

验证级别
行为
严格
(默认) MongoDB对所有文档的插入和更新应用相同的验证规则。
适度
MongoDB对文档的插入和更新应用相同的验证规则,这些更新与验证规则匹配的现有有效文档。对于不匹配验证规则的集合中现有文档的更新不需要通过验证。

本页上的示例使用一个包含以下文档的contacts集合

db.contacts.insertMany([
{ "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
{ "_id": 2, "name": "Ivan", "city": "Vancouver" }
])

以下示例向 contacts 集合添加了 严格 验证,并显示了尝试更新无效文档时的结果。

1

使用 strict 验证级别向 contacts 集合添加验证器。

db.runCommand( {
collMod: "contacts",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "phone must be a string and is required"
},
name: {
bsonType: "string",
description: "name must be a string and is required"
}
}
} },
validationLevel: "strict"
} )

由于 validationLevel严格,当任何文档更新时,MongoDB 都会检查该文档是否符合验证规则。

2

以下更新命令修改了 contacts 集合中的两个文档,使得这两个文档都不符合要求 name 必须是字符串的验证规则。

db.contacts.updateOne(
{ _id: 1 },
{ $set: { name: 10 } }
)
db.contacts.updateOne(
{ _id: 2 },
{ $set: { name: 20 } }
)
3

两次更新操作都失败了。MongoDB为每个操作返回以下输出

MongoServerError: Document failed validation
Additional information: {
failingDocumentId: <id>,
details: {
operatorName: '$jsonSchema',
schemaRulesNotSatisfied: [
{
operatorName: 'properties',
propertiesNotSatisfied: [
{
propertyName: 'name',
description: 'name must be a string and is required',
details: [
{
operatorName: 'bsonType',
specifiedAs: { bsonType: 'string' },
reason: 'type did not match',
consideredValue: <value>,
consideredType: 'int'
}
]
}
]
},
{
operatorName: 'required',
specifiedAs: { required: [ 'phone', 'name' ] },
missingProperties: [ 'phone' ]
}
]
}
}

以下示例向 contacts 集合添加了一个 moderate 验证,并在尝试更新无效文档时显示结果。

1

使用 moderate 验证级别向 contacts 集合添加验证器

db.runCommand( {
collMod: "contacts",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "phone must be a string and is required"
},
name: {
bsonType: "string",
description: "name must be a string and is required"
}
}
} },
validationLevel: "moderate"
} )

因为 validationLevelmoderate

  • 如果您使用 _id: 1 更新文档,MongoDB 将应用新的验证规则,因为现有文档符合验证要求。

  • 如果您使用 _id: 2 更新文档,MongoDB 不会应用新的验证规则,因为现有文档不符合验证要求。

2

以下更新命令修改了 contacts 集合中的两个文档,使得这两个文档都不符合要求 name 必须是字符串的验证规则。

db.contacts.updateOne(
{ _id: 1 },
{ $set: { name: 10 } }
)
db.contacts.updateOne(
{ _id: 2 },
{ $set: { name: 20 } }
)
3

MongoDB 对每个操作返回以下输出

// _id: 1
MongoServerError: Document failed validation
Additional information: {
failingDocumentId: 1,
details: {
operatorName: '$jsonSchema',
schemaRulesNotSatisfied: [
{
operatorName: 'properties',
propertiesNotSatisfied: [
{
propertyName: 'name',
description: 'name must be a string and is required',
details: [
{
operatorName: 'bsonType',
specifiedAs: { bsonType: 'string' },
reason: 'type did not match',
consideredValue: 10,
consideredType: 'int'
}
]
}
]
}
]
}
}
// _id: 2
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 0,
upsertedCount: 0
}

输出显示

  • 带有 _id: 1 的文档更新失败。该文档符合初始验证要求,MongoDB 对该文档应用验证规则。

  • 带有 _id: 2 的文档更新成功。该文档不符合初始验证要求,MongoDB 不对该文档应用验证规则。

重要

错误输出旨在供人类使用。它可能会在未来更改,不应在脚本中依赖。

  • 选择如何处理无效文档

  • 修改模式验证

返回

指定查询运算符