$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 specificationMongoDB支持JSONSchema的草案4,包括核心规范 and validation specification
, with some differences.
和验证规范
,但有一些不同。
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. MongoDB实现了JSONSchema中可用的键子集。For a complete list of omissions, see Omissions.有关遗漏的完整列表,请参阅遗漏。
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 | array of JSON Schema objects | |
anyOf | all types | array of JSON Schema objects | |
bsonType | all types | string alias or array of string aliases | $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. Otherwise, it is an inclusive 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 | a JSON Schema object | |
oneOf | all types | array of JSON Schema objects | |
pattern | strings | string containing a regex | |
patternProperties | objects | object | properties requirements, each property name of this object must be a valid regular expressionproperties 要求外,此对象的每个属性名称都必须是有效的正则表达式 |
properties | objects | object | |
required | objects | array of unique strings | |
title | N/A | string | |
type | all types | string or array of unique strings | "object" 、"array" 、"number" 、"boolean" 、"string" 和"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
键,该键允许您在$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 definitionsJSON模式规范草案4中的超文本定义。in draft 4 of the JSON Schema spec.
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
的示例,请参阅以下页面: