Definition定义
$jsonSchemaThe$jsonSchemaoperator matches documents that satisfy the specified JSON Schema.$jsonSchema运算符匹配满足指定JSON模式的文档。
Syntax语法
The $jsonSchema operator expression has the following syntax:$jsonSchema运算符表达式具有以下语法:
{ $jsonSchema: <JSON Schema object> }
Where the JSON Schema object is formatted according to draft 4 of the JSON Schema standard.JSON Schema对象根据JSON Schema标准的草案4进行格式化。
{ <keyword1>: <value1>, ... }
For example:例如:
{
$jsonSchema: {
required: [ "name", "major", "gpa", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
address: {
bsonType: "object",
required: [ "zipcode" ],
properties: {
"street": { bsonType: "string" },
"zipcode": { bsonType: "string" }
}
}
}
}
}JSON Schema模式
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences. For details, see Extensions and Omissions.MongoDB支持JSON Schema的草案4,包括核心规范和验证规范,但存在一些差异。有关详细信息,请参阅扩展和省略。
For more information about JSON Schema, see the official website.有关JSON Schema的更多信息,请参阅官方网站。
Available Keywords可用键
You can specify the following keywords in your JSON Schema.您可以在JSON模式中指定以下键。
Note
MongoDB implements a subset of keywords available in JSON Schema. For a complete list of omissions, see Omissions.MongoDB实现了JSON模式中可用的键子集。有关遗漏的完整列表,请参阅遗漏。
additionalItems | |||
additionalProperties |
| ||
allOf | |||
anyOf | |||
bsonType | $type operator$type运算符使用的字符串别名相同的字符串别名 | ||
dependencies | |||
description | N/A | description field is specified, MongoDB includes the description in the error output when a document fails validation.description字段,当文档验证失败时,MongoDB会在错误输出中包含description。 | |
enum | |||
exclusiveMaximum | numbers | maximum is an exclusive maximum. Otherwise, it is an inclusive maximum.maximum为排除性的最大值。否则,这是一个包容性的最大值。 | |
exclusiveMinimum | numbers | minimum is an exclusive minimum. Otherwise, it is an inclusive minimum.minimum是排除性的最小值。否则,这是一个包容性的最小值。 | |
items | arrays | ||
maximum | numbers | ||
maxItems | arrays | ||
maxLength | strings | ||
maxProperties | objects | ||
minimum | numbers | ||
minItems | arrays | ||
minLength | strings | ||
minProperties | objects | ||
multipleOf | numbers | ||
not | |||
oneOf | |||
pattern | strings | ||
patternProperties | properties requirements, each property name of this object must be a valid regular expressionproperties要求外,此对象的每个属性名称都必须是有效的正则表达式 | ||
properties | objects | object | |
required | objects | ||
title | N/A | ||
type |
| ||
uniqueItems | arrays | true,则数组中的每个项都必须是唯一的。否则,不会强制执行唯一性约束。 |
Extensions扩展
MongoDB's implementation of JSON Schema includes the addition of the MongoDB对JSON Schema的实现包括添加bsonType keyword, which allows you to use all BSON types in the $jsonSchema operator. bsonType accepts the same string aliases used for the $type operator.bsonType键,这允许您在$jsonSchema运算符中使用所有BSON类型。bsonType接受与$type运算符使用的字符串别名相同的别名。
Omissions遗漏
The following are not supported in MongoDB's implementation of JSON Schema:MongoDB的JSON Schema实现不支持以下内容:
Hypertext definitions in draft 4 of the JSON Schema spec.JSON模式规范草案4中的超文本定义。The keywords:关键词:$ref$schemadefaultdefinitionsformatid
Theintegertype. You must use the BSON typeintorlongwith thebsonTypekeyword.integer类型。您必须将BSON类型int或long与bsonType键一起使用。Hypermedia and linking properties of JSON Schema, including the use of JSON References and JSON Pointers.JSON模式的超媒体和链接属性,包括JSON引用和JSON游标的使用。Unknown keywords.未知键。
Examples示例
For examples using 有关使用$jsonSchema, see the following pages:$jsonSchema的示例,请参阅以下页面: