Docs HomeMongoDB Manual

$jsonSchema

Definition定义

$jsonSchema

The $jsonSchema operator 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. MongoDB支持JSONSchema的草案4,包括核心规范验证规范,但有一些不同。For details, see Extensions and Omissions.有关详细信息,请参阅扩展省略

For more information about JSON Schema, see the official website.有关JSON模式的更多信息,请参阅官方网站

Available Keywords可用键

You can specify the following keywords in your JSON Schema.您可以在JSON模式中指定以下键。

Note

MongoDB implements a subset of keywords available in JSON Schema. MongoDB实现了JSONSchema中可用的键子集。For a complete list of omissions, see Omissions.有关遗漏的完整列表,请参阅遗漏

Keyword关键字Type类型Definition定义Behavior行为
additionalItemsarraysboolean or objectIf an object, must be a valid JSON Schema如果是对象,则必须是有效的JSON架构
additionalPropertiesobjectsboolean or objectIf true, additional fields are allowed. If false, they are not. 如果为true,则允许使用其他字段。如果是false,则不是。If a valid JSON Schema object is specified, additional fields must validate against the schema.如果指定了有效的JSON模式对象,则必须根据该模式验证其他字段。
Defaults to true. 默认为true
allOfall typesarray of JSON Schema objectsField must match all specified schemas字段必须与所有指定的架构匹配
anyOfall typesarray of JSON Schema objectsField must match at least one of the specified schemas字段必须至少与一个指定的架构匹配
bsonTypeall typesstring alias or array of string aliasesAccepts same string aliases used for the $type operator接受用于$type运算符的相同字符串别名
dependenciesobjectsobjectDescribes field or schema dependencies描述字段或架构依赖关系
descriptionN/AstringA string that describes the schema and has no effect on validation. 描述架构并且对验证没有影响的字符串。Starting in MongoDB 5.1, if the description field is specified, MongoDB includes the description in the error output when a document fails validation.从MongoDB 5.1开始,如果指定了description字段,当文档验证失败时,MongoDB会在错误输出中包含描述。
enumall typesarray of valuesEnumerates all possible values of the field枚举字段的所有可能值
exclusiveMaximumnumbersbooleanIf true and field is a number, maximum is an exclusive maximum. 若为true并且字段是一个数字,则maximum是一个独占的maximumOtherwise, it is an inclusive maximum.否则,它是一个包含的最大值。
exclusiveMinimumnumbersbooleanIf true, minimum is an exclusive minimum. Otherwise, it is an inclusive minimum.如果为true,则minimum为排他极小值。否则,它是一个包含最小值。
itemsarraysobject or arrayMust be either a valid JSON Schema, or an array of valid JSON Schemas必须是有效的JSON架构或有效的JSON模式数组
maximumnumbersnumberIndicates the maximum value of the field指示字段的最大值
maxItemsarraysintegerIndicates the maximum length of array指示数组的最大长度
maxLengthstringsintegerIndicates the maximum length of the field指示字段的最大长度
maxPropertiesobjectsintegerIndicates the field's maximum number of properties指示字段的最大属性数
minimumnumbersnumberIndicates the minimum value of the field指示字段的最小值
minItemsarraysintegerIndicates the minimum length of array指示数组的最小长度
minLengthstringsintegerIndicates the minimum length of the field指示字段的最小长度
minPropertiesobjectsintegerIndicates the field's minimum number of properties指示字段的最小属性数
multipleOfnumbersnumberField must be a multiple of this value字段必须是该值的倍数
notall typesa JSON Schema objectField must not match the schema字段必须与架构不匹配
oneOfall typesarray of JSON Schema objectsField must match exactly one of the specified schemas字段必须与指定的架构之一完全匹配
patternstringsstring containing a regexField must match the regular expression字段必须与正则表达式匹配
patternPropertiesobjectsobjectIn addition to properties requirements, each property name of this object must be a valid regular expression除了properties要求外,此对象的每个属性名称都必须是有效的正则表达式
propertiesobjectsobjectA valid JSON Schema where each value is also a valid JSON Schema object有效的JSON模式,其中每个值也是有效的JSON架构对象
requiredobjectsarray of unique stringsObject's property set must contain all the specified elements in the array对象的属性集必须包含数组中所有指定的元素
titleN/AstringA descriptive title string with no effect.无效的描述性标题字符串。
typeall typesstring or array of unique stringsEnumerates the possible JSON types of the field. 枚举字段的可能JSON类型。Available types are "object", "array", "number", "boolean", "string", and "null".可用的类型是"object""array""number""boolean""string""null"
MongoDB's implementation of the JSON Schema does not support the "integer" type. MongoDB对JSON模式的实现不支持“integer”类型。Use the bsonType keyword and the "int" or "long" types instead. 请改用bsonType键和“int”或“long”类型。
uniqueItemsarraysbooleanIf true, each item in the array must be unique. 如果为true,则数组中的每个项都必须是唯一的。Otherwise, no uniqueness constraint is enforced.否则,不会强制执行唯一性约束。

Extensions扩展

MongoDB's implementation of JSON Schema includes the addition of the bsonType keyword, which allows you to use all BSON types in the $jsonSchema operator. MongoDB对jsonSchema的实现包括添加bsonType键,该键允许您在$jsonSchema运算符中使用所有BSON类型。bsonType accepts the same string aliases used for the $type operator.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
    • $schema
    • default
    • definitions
    • format
    • id
  • The integer type. integer类型。You must use the BSON type int or long with the bsonType keyword.必须将BSON类型intlongbsonType键一起使用。
  • 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的示例,请参阅以下页面: