Database Manual / Reference / Query Language / Query Predicates / Miscellaneous

$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. 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模式中可用的键子集。有关遗漏的完整列表,请参阅遗漏

Keyword关键词Type类型Definition定义Behavior行为
additionalItemsarrays数组boolean or object布尔值或对象If an object, must be a valid JSON Schema如果是对象,则必须是有效的JSON模式
additionalPropertiesobjects物体boolean or object布尔值或对象

If true, additional fields are allowed. If false, they are not. If a valid JSON Schema object is specified, additional fields must validate against the schema.如果为true,则允许添加其他字段。如果是假的,那就不是。如果指定了有效的JSON模式对象,则其他字段必须根据模式进行验证。

Defaults to true.默认为true

allOfall types所有类型array of JSON Schema objectsJSON架构对象数组Field must match all specified schemas字段必须与所有指定的架构匹配
anyOfall types所有类型array of JSON Schema objectsJSON架构对象数组Field must match at least one of the specified schemas字段必须至少匹配一个指定的架构
bsonTypeall types所有类型string alias or array of string aliases字符串别名或字符串别名数组Accepts same string aliases used for the $type operator接受与$type运算符使用的字符串别名相同的字符串别名
dependenciesobjects对象object对象Describes field or schema dependencies描述字段或架构依赖关系
descriptionN/Astring字符串A 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会在错误输出中包含description
enumall types所有类型array of values值数组Enumerates all possible values of the field枚举字段的所有可能值
exclusiveMaximumnumbersboolean布尔值If true and field is a number, maximum is an exclusive maximum. Otherwise, it is an inclusive maximum.若为真且字段为数字,则maximum为排除性的最大值。否则,这是一个包容性的最大值。
exclusiveMinimumnumbersboolean布尔值If true, minimum is an exclusive minimum. Otherwise, it is an inclusive minimum.如果为真,则minimum是排除性的最小值。否则,这是一个包容性的最小值。
itemsarraysobject or array对象或数组Must be either a valid JSON Schema, or an array of valid JSON Schemas必须是有效的JSON模式,或者是有效JSON模式的数组
maximumnumbersnumber数字Indicates the maximum value of the field指示字段的最大值
maxItemsarraysinteger整数Indicates the maximum length of array表示数组的最大长度
maxLengthstringsinteger整数Indicates the maximum length of the field指示字段的最大长度
maxPropertiesobjectsinteger整数Indicates the field's maximum number of properties指示字段的最大属性数
minimumnumbersnumber数字Indicates the minimum value of the field指示字段的最小值
minItemsarraysinteger整数Indicates the minimum length of array表示数组的最小长度
minLengthstringsinteger整数Indicates the minimum length of the field指示字段的最小长度
minPropertiesobjectsinteger整数Indicates the field's minimum number of properties指示字段的最小属性数
multipleOfnumbersnumber数字Field must be a multiple of this value字段必须是此值的倍数
notall types所有类型a JSON Schema objectJSON模式对象Field must not match the schema字段不得与架构匹配
oneOfall types所有类型array of JSON Schema objectsJSON模式对象数组Field must match exactly one of the specified schemas字段必须与指定的架构之一完全匹配
patternstringsstring containing a regex包含正则表达式的字符串Field must match the regular expression字段必须与正则表达式匹配
patternPropertiesobjects对象object对象In 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 strings唯一字符串数组Object's property set must contain all the specified elements in the array对象的属性集必须包含数组中的所有指定元素
titleN/Astring字符串A descriptive title string with no effect.一个没有效果的描述性标题字符串。
typeall types所有类型string or array of unique strings唯一字符串的字符串或数组

Enumerates the possible JSON types of the field. Available types are "object", "array", "number", "boolean", "string", and "null".枚举字段的可能JSON类型。可用类型有“对象”、“数组”、“数字”、“布尔值”、“字符串”和“null”。

MongoDB's implementation of the JSON Schema does not support the "integer" type. Use the bsonType keyword and the "int" or "long" types instead.MongoDB的JSON模式实现不支持“整数”类型。请使用bsonType键和“int”或“long”类型。

uniqueItemsarraysboolean布尔值If true, each item in the array must be unique. Otherwise, no uniqueness constraint is enforced.如果为true,则数组中的每个项都必须是唯一的。否则,不会强制执行唯一性约束。

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. bsonType accepts the same string aliases used for the $type operator.MongoDB对JSON Schema的实现包括添加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
    • $schema
    • default
    • definitions
    • format
    • id
  • The integer type. You must use the BSON type int or long with the bsonType keyword.integer类型。您必须将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的示例,请参阅以下页面: