Database Manual / Data Modeling / Schema Validation / Specify JSON Validation

Tips for JSON Schema ValidationJSON模式验证提示

This page describes best practices for JSON schema validation to help avoid common issues.本页介绍JSON模式验证的最佳实践,以帮助避免常见问题。

_id Field and 字段和additionalProperties: false

When you specify additionalProperties: false in your JSON schema, MongoDB rejects documents that contain fields not included in your schema's properties object.当您在JSON模式中指定additionalProperties: false时,MongoDB会拒绝包含未包含在模式properties对象中的字段的文档。

Because all objects contain an automatically-generated _id field, when you set additionalProperties: false, you must include the _id field in your properties object. If you don't, all documents are rejected.因为所有对象都包含一个自动生成的_id字段,所以当您设置additionalProperties: false时,您必须在属性对象中包含_id字段。如果你不这样做,所有文件都会被拒绝。

For example, with this validation, no documents are valid:例如,通过此验证,没有有效的文档:

{
"$jsonSchema": {
"required": [ "_id", "storeLocation" ],
"properties": {
"storeLocation": { "bsonType": "string" }
},
"additionalProperties": false
}
}

This validation ensures that storeLocation is a string. However, the properties object does not contain an _id field.此验证确保storeLocation是一个字符串。但是,properties对象不包含_id字段。

To allow documents in the collection, you must update the properties object to include an _id field:要允许集合中的文档,您必须更新properties对象以包含_id字段:

{
"$jsonSchema": {
"required": [ "_id", "storeLocation" ],
"properties": {
"_id": { "bsonType": "objectId" },
"storeLocation": { "bsonType": "string" }
},
"additionalProperties": false
}
}

Validation for null Field Valuesnull字段值的验证

Your application may be configured to set missing field values to null, instead of not including those fields in the object sent to the collection.应用程序可能被配置为将缺少的字段值设置为null,而不是在发送到集合的对象中不包含这些字段。

If your schema validates data types for a field, to insert documents with a null value for that field, you must explicitly allow null as a valid BSON type.如果架构验证了字段的数据类型,要插入该字段值为null的文档,您必须明确允许null作为有效的BSON类型。

For example, this schema validation does not allow documents where storeLocation is null:例如,此模式验证不允许storeLocationnull的文档:

db.createCollection("sales",
{
validator:
{
"$jsonSchema": {
"properties": {
"storeLocation": { "bsonType": "string" }
}
}
}
}
)

With the preceding validation, this document is rejected:经过上述验证,本文件被拒绝:

db.store.insertOne( { storeLocation: null } )

Alternatively, this schema validation allows null values for storeLocation:或者,此模式验证允许storeLocationnull值:

db.createCollection("store",
{
validator:
{
"$jsonSchema": {
"properties": {
"storeLocation": { "bsonType": [ "null", "string" ] }
}
}
}
}
)

With the preceding validation, this document is allowed:经过上述验证,本文件允许:

db.store.insertOne( { storeLocation: null } )

Note

null Fields Compared with Missing Fields空字段与缺失字段的比较

null field values are not the same as missing fields. If a field is missing from a document, MongoDB does not validate that field.null字段值与缺少的字段不同。如果文档中缺少字段,MongoDB不会验证该字段。

Validation with Encrypted Fields使用加密字段进行验证

If you have Client-Side Field Level Encryption or Queryable Encryption enabled on a collection, validation is subject to the following restrictions:如果在集合上启用了客户端字段级加密可查询加密,则验证受以下限制:

  • For CSFLE, when running collMod, the libmongocrypt library prefers the the JSON encryption schema specified in the command. 对于CSFLE,运行collMod时,libmongocrypt库更喜欢命令中指定的JSON加密模式This enables setting a schema on a collection that does not yet have one.这允许在还没有架构的集合上设置架构。
  • For Queryable Encryption, any JSON schema that includes an encrypted field results in a query analysis error.对于可查询加密,任何包含加密字段的JSON模式都会导致查询分析错误。