On this page
Schema Validation
在本页面
3.2 版中的新功能。
MongoDB 提供了在更新和插入期间执行架构验证的功能。
指定验证规则
验证规则基于每个 collections。
要在创建新集合时指定验证规则,请使用db.createCollection()和validator
选项。
要将文档验证添加到现有集合,请使用collMod命令和validator
选项。
MongoDB 还提供以下相关选项:
validationLevel
选项,该选项确定 MongoDB 在更新期间将验证规则严格应用于现有文档的方式,以及validationAction
选项,该选项确定 MongoDB 是应该error
并拒绝违反验证规则的文档,还是拒绝warn
关于日志中的违规但允许无效文档的文档。
JSON Schema
3.6 版的新功能。
从 3.6 版开始,MongoDB 支持 JSON 模式验证。要指定 JSON 模式验证,请在validator
表达式中使用$jsonSchema运算符。
Note
推荐使用 JSON 模式进行模式验证。
例如,以下示例使用 JSON 模式指定验证规则:
db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
"description": "must be a string and is required"
}
}
}
}
}
}
})
有关更多信息,请参见$jsonSchema。
其他查询表达式
除了使用$jsonSchema查询运算符的 JSON 模式验证外,MongoDB 还支持使用其他查询运算符进行验证,但$near,$nearSphere,$text和$where运算符除外。
例如,以下示例使用查询表达式指定验证器规则:
db.createCollection( "contacts",
{ validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )
See also
Behavior
验证在更新和插入期间进行。将验证添加到集合时,现有文档将不进行验证检查,直到进行修改。
Existing Documents
validationLevel
选项确定 MongoDB 应用哪些操作验证规则:
如果
validationLevel
为strict
(默认值),则 MongoDB 将验证规则应用于所有插入和更新。如果
validationLevel
为moderate
,则 MongoDB 将验证规则应用于已满足验证条件的现有文档的插入和更新。在moderate
级别,不检查对不满足验证条件的现有文档的更新的有效性。
例如,使用以下文档创建一个contacts
集合:
db.contacts.insert([
{ "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
{ "_id": 2, "name": "Ivan", "city": "Vancouver" }
])
发出以下命令以将验证器添加到contacts
集合:
db.runCommand( {
collMod: "contacts",
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone", "name" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
name: {
bsonType: "string",
description: "must be a string and is required"
}
}
} },
validationLevel: "moderate"
} )
contacts
集合现在具有一个具有moderate
validationLevel 的验证器:
如果您尝试使用
_id
/1
更新文档,则 MongoDB 将应用验证规则,因为现有文档符合条件。相反,MongoDB 不会将验证规则应用于
_id
到2
的文档更新,因为它不符合验证规则。
要完全禁用验证,可以将validationLevel
设置为off
。
接受或拒绝无效的文档
validationAction
选项确定 MongoDB 如何处理违反验证规则的文档:
如果
validationAction
为error
(默认值),则 MongoDB 拒绝任何违反验证条件的插入或更新。如果
validationAction
为warn
,则 MongoDB 会记录任何违规情况,但允许进行插入或更新。
例如,使用以下 JSON 模式验证器创建contacts2
集合:
db.createCollection( "contacts2", {
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType : "string",
pattern : "@mongodb\.com$",
description: "must be a string and match the regular expression pattern"
},
status: {
enum: [ "Unknown", "Incomplete" ],
description: "can only be one of the enum values"
}
}
} },
validationAction: "warn"
} )
使用warn
validationAction,MongoDB 会记录所有违规,但允许进行插入或更新。
例如,以下插入操作违反了验证规则:
db.contacts2.insert( { name: "Amanda", status: "Updated" } )
但是,由于validationAction
仅是warn
,因此 MongoDB 仅记录验证冲突消息,并允许操作 continue 进行:
2017-12-01T12:31:23.738-0500 W STORAGE [conn1] Document would fail validation collection: example.contacts2 doc: { _id: ObjectId('5a2191ebacbbfc2bdc4dcffc'), name: "Amanda", status: "Updated" }
Restrictions
您无法为admin
,local
和config
数据库中的集合指定验证器。
您无法为system.*
个集合指定验证器。
绕过文档验证
用户可以使用bypassDocumentValidation
选项绕过文档验证。有关支持bypassDocumentValidation
选项的命令列表,请参见Document Validation。
对于已启用访问控制的部署,要绕过文档验证,已认证的用户必须具有bypassDocumentValidation操作。内置角色dbAdmin和restore提供此操作。
Additional Information
See also