“文档” 菜单
文档首页
/
MongoDB Manual
/ /

指定现有文档的验证级别

在此页面上

  • 上下文
  • 先决条件
  • 步骤:使用 strict验证
  • 步骤:使用 moderate 验证
  • 了解详情

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

模式的 validationLevel决定 MongoDB 应用验证规则的文档:

validationLevel
行为
strict
默认)MongoDB 对所有文档插入和更新应用相同的验证规则。
moderate
MongoDB 将相同的验证规则应用于文档插入和对符合验证规则的现有有效文档的更新。对集合中不符合验证规则的现有文档的更新不会进行有效性检查。

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

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

以下示例将 strict 验证添加到 contacts 集合,并在尝试更新无效文档时显示结果。

1

使用 strict validationLevelcontacts 集合添加一个验证器:

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

由于validationLevelstrict ,因此当更新任何文档时,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' ]
}
]
}
}

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

1

使用 moderate validationLevelcontacts 集合添加一个验证器:

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 不会对此文档应用验证规则。

重要

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

  • 选择如何处理无效文档

  • 修改模式验证

← 修改模式验证