Modify Schema Validation修改架构验证
On this page本页内容
After you add schema validation to a collection, you can modify validation rules at any time. 将架构验证添加到集合后,可以随时修改验证规则。For example, you may decide:例如,您可以决定:
That documents in ausers
collection no longer require an email address.users
集合中的文档不再需要电子邮件地址。To increase the minimum length for a将password
field from 8 characters to 12.password
字段的最小长度从8个字符增加到12个字符。
To modify a collection's schema validation, use the 要修改集合的架构验证,请使用collMod
command and specify the updated validation in the validator
object.collMod
命令并在validator
对象中指定更新的验证。
About This Task关于此任务
You can modify all components of a schema validation, including its rules, validation level, and validation action.您可以修改架构验证的所有组件,包括其规则、验证级别和验证操作。
If you update a collection's validation rules, documents inserted prior to the validation change may no longer be valid. 如果更新集合的验证规则,则在验证更改之前插入的文档可能不再有效。How MongoDB handles these invalid documents depends on your MongoDB如何处理这些无效文档取决于您的validationLevel
. validationLevel
。By default, MongoDB applies validation checks to all documents, regardless of when they were inserted.默认情况下,MongoDB将验证检查应用于所有文档,无论它们是何时插入的。
Steps步骤
The following procedure creates a collection with validation rules and then modifies those rules.以下过程使用验证规则创建集合,然后修改这些规则。
Create a collection with validation.创建一个经过验证的集合。
Create a 使用验证规则创建users
collection with validation rules:users
集合:
db.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "username", "password" ],
properties: {
username: {
bsonType: "string",
description: "must be a string and is required"
},
password: {
bsonType: "string",
minLength: 8,
description: "must be a string at least 8 characters long, and is required"
}
}
}
}
} )
Modify the validation schema.修改验证架构。
Run the following 运行以下collMod
command to change the minLength
of the password
field from 8 to 12:collMod
命令将密码字段的minLength
从8
更改为12
:
db.runCommand( { collMod: "users",
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "username", "password" ],
properties: {
username: {
bsonType: "string",
description: "must be a string and is required"
},
password: {
bsonType: "string",
minLength: 12,
description: "must be a string of at least 12 characters, and is required"
}
}
}
}
} )
You can also use the 您还可以使用collMod
command to add validation to an existing collection that was not created with validation.collMod
命令向未通过验证创建的现有集合添加验证。
Results结果
The following sections show the results of the updated validation in these scenarios:以下部分显示了这些场景中更新验证的结果:
When you insert an invalid document.插入无效文档时。When you insert a valid document.插入有效文档时。When a previously valid document becomes invalid because of the validation rule changes.当以前有效的文档由于验证规则更改而变得无效时。
Insert an Invalid Document插入无效文档
The following operation attempts to insert an invalid document. 以下操作试图插入无效文档。The document is invalid because the 文档无效,因为password
field is 10 characters long when the minimum length is 12:password
字段的长度为10个字符,而最小长度为12:
db.users.insertOne(
{
"username": "salesAdmin01",
"password": "kT9$j4wg#M"
}
)
MongoDB returns the following error:MongoDB返回以下错误:
MongoServerError: Document failed validation
Additional information: {
failingDocumentId: ObjectId("62be0adb73c105dde9231299"),
details: {
operatorName: '$jsonSchema',
schemaRulesNotSatisfied: [
{
operatorName: 'properties',
propertiesNotSatisfied: [
{
propertyName: 'password',
description: 'must be a string of at least 8 characters, and is required',
details: [
{
operatorName: 'minLength',
specifiedAs: { minLength: 12 },
reason: 'specified string length was not satisfied',
consideredValue: 'kT9$j4wg#M'
}
]
}
]
}
]
}
}
Insert a Valid Document插入有效文档
The following operation inserts a valid document, where the 以下操作将插入一个有效文档,其中password
field is at least 12 characters long:password
字段的长度至少为12个字符:
db.users.insertOne(
{
"username": "salesAdmin01",
"password": "8p&SQd7T90$KKx"
}
)
Handle a Previously Valid Document That Is No Longer Valid处理不再有效的以前有效的文档
Consider the following document that is valid for the first version of the schema validation, but not the second:请考虑以下文档,该文档对架构验证的第一个版本有效,但对第二个版本无效:
db.users.insertOne(
{
"username": "salesAdmin02",
"password": "i8U60*VyL8"
}
)
The document's 文档的password
field is 10 characters. password
字段为10个字符。The first version of the schema validation required a minimum of 8 characters, meaning this document was valid. 架构验证的第一个版本至少需要8个字符,这意味着此文档是有效的。However, after updating the validation to require the 但是,在更新验证以要求password
to be a minimum of 12 characters, the document is no longer valid.password
至少为12个字符之后,文档将不再有效。
When a change in schema validation causes previously valid documents to become invalid, the newly invalid documents remain in the collection.当架构验证的更改导致以前有效的文档变为无效时,新无效的文档将保留在集合中。
The way MongoDB handles newly invalid documents depends on the schema's MongoDB处理新无效文档的方式取决于模式的validationLevel
. validationLevel
。The schema validation in this example uses the default 本例中的模式验证使用了validationLevel
of strict
, meaning the document must match the new validation rules. strict
的默认validationLevel
,这意味着文档必须与新的验证规则相匹配。MongoDB checks the validation each time the document is updated.每次文档更新时,MongoDB都会检查验证。
If the updated schema validation had a 如果更新的模式验证的validationLevel
of moderate
, this document would not need to match the new validation rules.validationLevel
为moderate
,则此文档将不需要匹配新的验证规则。