On this page本页内容
$jsonSchema
¶New in version 3.6.3.6版中的新增。
The $jsonSchema
operator matches documents that satisfy the specified JSON Schema.$jsonSchema
运算符匹配满足指定JSON架构的文档。
The $jsonSchema
operator expression has the following syntax:$jsonSchema
运算符表达式具有以下语法:
Where the JSON Schema object is formatted according to draft 4 of the JSON Schema standard.其中,JSON架构对象根据JSON架构标准草案4进行格式化。
For example:例如:
For a list of keywords supported by MongoDB, see Available Keywords.有关MongoDB支持的关键字列表,请参阅可用关键字。
Note
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences.MongoDB支持JSON架构草案4,包括核心规范和校验规范,但有一些不同。See Extensions and Omissions for details.有关详细信息,请参阅扩展和省略。
For more information about JSON Schema, see the official website.有关JSON架构的更多信息,请参阅官方网站。
The featureCompatibilityVersion must be set to 要使用"3.6"
or higher in order to use $jsonSchema
.$jsonSchema
,必须将featureCompatibilityVersion
设置为“3.6”或更高。
You can use 您可以在文档校验器中使用$jsonSchema
in a document validator to enforce the specified schema on insert and update operations:$jsonSchema
在插入和更新操作中强制执行指定的架构:
You can use 您可以在读写操作的查询条件中使用$jsonSchema
in query conditions for read and write operations to find documents in the collection that satisfy the specified schema:$jsonSchema
来查找集合中满足指定架构的文档:
To find documents in the collection that do not satisfy the specified schema, use the 要查找集合中不满足指定架构的文档,请在$jsonSchema
expression in a $nor
expression.$nor
表达式中使用$jsonSchema
表达式。For example:例如:
The following 以下db.createCollection()
method creates a collection named students
and uses the $jsonSchema
operator to set schema validation rules:db.createCollection()
方法创建名为students
的集合,并使用$jsonSchema
运算符设置架构校验规则:
Given the created 给定为集合创建的validator
for the collection, the following insert operation will fail because gpa
is an integer when the validator
requires a double
.validator
,以下插入操作将失败,因为validator
需要double
,而gpa
是整数。
The operation returns the following error:该操作返回以下错误:
After changing the 将gpa
to a double, the insert succeeds:gpa
更改为双精度后,插入成功:
The operation returns the following:该操作返回以下内容:
You can use 您可以在读写操作的查询条件中使用$jsonSchema
in query conditions for read and write operations to find documents in the collection that satisfy the specified schema.$jsonSchema
来查找集合中满足指定架构的文档。
For example, create a sample collection 例如,使用以下文档创建样本集合inventory
with the following documents:inventory
:
Next, define the following sample schema object:接下来,定义以下示例架构对象:
You can use 您可以使用$jsonSchema
to find all documents in the collection that satisfy the schema:$jsonSchema
查找集合中满足架构的所有文档:
You can use 您可以将$jsonSchema
with the $nor
to find all documents that do not satisfy the schema:$jsonSchema
与$nor
一起使用,以查找所有不符合架构的文档:
Or, you can update all documents that do not satisfy the schema:或者,您可以更新所有不满足架构的文档:
Or, you can delete all documents that do not satisfy the schema:或者,您可以删除所有不符合架构的文档:
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences.MongoDB支持JSON架构草案4,包括核心规范和校验规范,但有一些不同。See Extensions and Omissions for details.有关详细信息,请参阅扩展和省略。
For more information about JSON Schema, see the official website.有关JSON架构的更多信息,请参阅官方网站。
Note
MongoDB implements a subset of keywords available in JSON Schema. For a complete list of omissions, see Omissions.MongoDB实现了JSON架构中可用的关键字子集。有关遗漏的完整列表,请参阅遗漏。
bsonType | $type operator$type 运算符相同的字符串别名 | ||
enum | |||
type |
| ||
allOf | |||
anyOf | |||
oneOf | |||
not | |||
multipleOf | numbers | number | |
maximum | numbers | number | |
exclusiveMaximum | numbers | boolean | maximum is an exclusive maximum. Otherwise, it is an inclusive maximum.true 且字段为数字,则maximum 为排除式最大值。否则,它是一个包含式最大值。 |
minimum | numbers | number | |
exclusiveMinimum | numbers | boolean | minimum is an exclusive minimum. Otherwise, it is an inclusive minimum.true ,则minimum 为排除式最小值。否则,它是一个包含式最小值。 |
maxLength | strings | integer | |
minLength | strings | integer | |
pattern | strings | ||
maxProperties | objects | integer | |
minProperties | objects | integer | |
required | objects | array of unique strings | |
additionalProperties | objects | boolean or object |
|
properties | objects | object | |
patternProperties | objects | object | properties requirements, each property name of this object must be a valid regular expressionproperties 要求外,此对象的每个属性名称必须是有效的正则表达式 |
dependencies | objects | object | |
additionalItems | arrays | boolean or object | |
items | arrays | object or array | |
maxItems | arrays | integer | |
minItems | arrays | integer | |
uniqueItems | arrays | boolean | true ,则数组中的每个项都必须是唯一的。否则,不会强制执行唯一性约束。 |
title | N/A | string | |
description | N/A | string |
MongoDB’s implementation of JSON Schema includes the addition of the MongoDB对JSON架构的实现包括添加bsonType
keyword, which allows you to use all BSON types in the $jsonSchema
operator.bsonType
关键字,它允许您在$jsonSchema
操作符中使用所有BSON类型。bsonType
accepts the same string aliases used for the $type
operator.bsonType
接受与$type
运算符相同的字符串别名。
The following are not supported in MongoDB’s implementation of JSON Schema:MongoDB的JSON架构实现不支持以下内容:
$ref
$schema
default
definitions
format
id
integer
type. You must use the BSON type int
or long
with the bsonType
keyword.integer
类型。必须将BSON类型int
或long
与bsonType
关键字一起使用。