$jsonSchema
On this page
Definition定义
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模式中指定以下关键字。
MongoDB implements a subset of keywords available in JSON Schema. For a complete list of omissions, see Omissions.MongoDB实现了JSONSchema中可用的关键字子集。有关遗漏的完整列表,请参阅遗漏。
additionalItems | arrays | boolean or object | |
additionalProperties | objects | boolean or object | true , additional fields are allowed. If false , they are not. true ,则允许使用其他字段。如果为false ,则不是。true . true 。 |
allOf | all types | ||
anyOf | all types | ||
bsonType | all types | $type operator$type 运算符的相同字符串别名 | |
dependencies | objects | object | |
description | N/A | string | description field is specified, MongoDB includes the description in the error output when a document fails validation.description 字段,当文档验证失败时,MongoDB会在错误输出中包含描述。 |
enum | all types | array of values | |
exclusiveMaximum | numbers | boolean | maximum is an exclusive maximum. true 并且maximum 字段是一个数字,则maximum 是一个排除的上限。 |
exclusiveMinimum | numbers | boolean | minimum is an exclusive minimum. true ,则minimum 为排除的下限。 |
items | arrays | object or array | |
maximum | numbers | number | |
maxItems | arrays | integer | |
maxLength | strings | integer | |
maxProperties | objects | integer | |
minimum | numbers | number | |
minItems | arrays | integer | |
minLength | strings | integer | |
minProperties | objects | integer | |
multipleOf | numbers | number | |
not | all types | ||
oneOf | all types | ||
pattern | strings | ||
patternProperties | objects | object | properties requirements, each property name of this object must be a valid regular expressionproperties 要求外,此对象的每个属性名称都必须是有效的正则表达式 |
properties | objects | object | |
required | objects | ||
title | N/A | string | |
type | all types | null ”。bsonType keyword and the "int" or "long" types instead. bsonType 关键字和“int”或“long”类型。 | |
uniqueItems | arrays | boolean | true ,则数组中的每个项都必须是唯一的。否则,不会强制执行唯一性约束。 |
Extensions扩展
MongoDB's implementation of JSON Schema includes the addition of the MongoDB对jsonSchema的实现包括添加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
-
$schema
-
default
-
definitions
-
format
-
id
-
-
Theinteger
type.integer
类型。You must use the BSON type必须将BSON类型int
orlong
with thebsonType
keyword.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
的示例,请参阅以下页面: