On this page本页内容
$jsonSchema
The $jsonSchema
operator matches documents that satisfy the specified JSON Schema.$jsonSchema
运算符匹配满足指定JSON模式的文档。
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模式对象根据JSON模式标准的草案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" } } } } } }
For a list of keywords supported by MongoDB, see Available Keywords.有关MongoDB支持的关键字列表,请参阅可用关键字。
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences. MongoDB支持JSON模式草案4,包括核心规范和验证规范,但存在一些差异。See Extensions and Omissions for details.有关详细信息,请参阅扩展和省略。
For more information about JSON Schema, see the official website.有关JSON模式的更多信息,请参阅官方网站。
The featureCompatibilityVersion
must be set to "3.6"
or higher in order to use $jsonSchema
.featureCompatibilityVersion
必须设置为"3.6"
或更高,才能使用$jsonSchema
。
You can use 您可以在文档验证器中使用$jsonSchema
in a document validator to enforce the specified schema on insert and update operations:$jsonSchema
在插入和更新操作中强制执行指定的模式:
db.createCollection( <collection>, { validator: { $jsonSchema: <schema> } } ) db.runCommand( { collMod: <collection>, validator:{ $jsonSchema: <schema> } } )
You can use 您可以在读写操作的查询条件中使用$jsonSchema
in query conditions for read and write operations to find documents in the collection that satisfy the specified schema:$jsonSchema
来查找集合中满足指定架构的文档:
db.collection.find( { $jsonSchema: <schema> } ) db.collection.aggregate( [ { $match: { $jsonSchema: <schema> } } ] ) db.collection.updateMany( { $jsonSchema: <schema> }, <update> ) db.collection.deleteOne( { $jsonSchema: <schema> } )
To find documents in the collection that do not satisfy the specified schema, use the 要查找集合中不满足指定架构的文档,请在$jsonSchema
expression in a $nor
expression. $nor
表达式中使用$jsonSchema
表达式。For example:例如:
db.collection.find( { $nor: [ { $jsonSchema: <schema> } ] } ) db.collection.aggregate( [ { $match: { $nor: [ { $jsonSchema: <schema> } ] } }, ... ] ) db.collection.updateMany( { $nor: [ { $jsonSchema: <schema> } ] }, <update> ) db.collection.deleteOne( { $nor: [ { $jsonSchema: <schema> } ] } )
The following 以下db.createCollection()
method creates a collection named students
and uses the $jsonSchema
operator to set schema validation rules:db.createCollection()
方法创建名为students
的集合,并使用$jsonSchema
运算符设置架构验证规则:
db.createCollection("students", { validator: { $jsonSchema: { bsonType: "object", required: [ "name", "year", "major", "address" ], properties: { name: { bsonType: "string", description: "must be a string and is required" }, year: { bsonType: "int", minimum: 2017, maximum: 3017, description: "must be an integer in [ 2017, 3017 ] and is required" }, major: { enum: [ "Math", "English", "Computer Science", "History", null ], description: "can only be one of the enum values and is required" }, gpa: { bsonType: [ "double" ], description: "must be a double if the field exists" }, address: { bsonType: "object", required: [ "city" ], properties: { street: { bsonType: "string", description: "must be a string if the field exists" }, city: { bsonType: "string", "description": "must be a string and is required" } } } } } } } )
Given the created 给定为集合创建的validator
for the collection, the following insert operation will fail because gpa
is an integer when the validator
requires a double
.validator
,以下插入操作将失败,因为当validator
需要double
时,gpa
是整数。
db.students.insertOne( { name: "Alice", year: Int32( 2019 ), major: "History", gpa: Int32( 3 ), address: { city: "NYC", street: "33rd Street" } } )
The operation returns the following error:操作返回以下错误:
MongoServerError: Document failed validation Additional information: { failingDocumentId: ObjectId("61aa577f666a50a8fccd7ec2"), details: { operatorName: '$jsonSchema', schemaRulesNotSatisfied: [ { operatorName: 'properties', propertiesNotSatisfied: [ { propertyName: 'gpa', description: 'must be a double if the field exists', details: [ [Object] ] } ] } ] } }
After changing the 将gpa
to a double, the insert succeeds:gpa
更改为double
后,插入成功:
db.students.insertOne( { name: "Alice", year: NumberInt(2019), major: "History", gpa: Double(3.0), address: { city: "NYC", street: "33rd Street" } } )
You can use 您可以在读写操作的查询条件中使用$jsonSchema
in query conditions for read and write operations to find documents in the collection that satisfy the specified schema.$jsonSchema
来查找集合中满足指定模式的文档。
For example, create a sample collection 例如,使用以下文档创建样本集合inventory
with the following documents:inventory
:
db.inventory.insertMany( [ { item: "journal", qty: NumberInt(25), size: { h: 14, w: 21, uom: "cm" }, instock: true }, { item: "notebook", qty: NumberInt(50), size: { h: 8.5, w: 11, uom: "in" }, instock: true }, { item: "paper", qty: NumberInt(100), size: { h: 8.5, w: 11, uom: "in" }, instock: 1 }, { item: "planner", qty: NumberInt(75), size: { h: 22.85, w: 30, uom: "cm" }, instock: 1 }, { item: "postcard", qty: NumberInt(45), size: { h: 10, w: 15.25, uom: "cm" }, instock: true }, { item: "apple", qty: NumberInt(45), status: "A", instock: true }, { item: "pears", qty: NumberInt(50), status: "A", instock: true } ] )
Next, define the following sample schema object:接下来,定义以下示例架构对象:
let myschema = { required: [ "item", "qty", "instock" ], properties: { item: { bsonType: "string" }, qty: { bsonType: "int" }, size: { bsonType: "object", required: [ "uom" ], properties: { uom: { bsonType: "string" }, h: { bsonType: "double" }, w: { bsonType: "double" } } }, instock: { bsonType: "bool" } } }
You can use 您可以使用$jsonSchema
to find all documents in the collection that satisfy the schema:$jsonSchema
查找集合中满足架构的所有文档:
db.inventory.find( { $jsonSchema: myschema } ) db.inventory.aggregate( [ { $match: { $jsonSchema: myschema } } ] )
You can use 您可以将$jsonSchema
with the $nor
to find all documents that do not satisfy the schema:$jsonSchema
与$nor
一起使用,以查找所有不满足架构的文档:
db.inventory.find( { $nor: [ { $jsonSchema: myschema } ] } )
Or, you can update all documents that do not satisfy the schema:或者,您可以更新所有不满足架构的文档:
db.inventory.updateMany( { $nor: [ { $jsonSchema: myschema } ] }, { $set: { isValid: false } } )
Or, you can delete all documents that do not satisfy the schema:或者,您可以删除所有不满足架构的文档:
db.inventory.deleteMany( { $nor: [ { $jsonSchema: myschema } ] } )
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences. MongoDB支持JSON模式草案4,包括核心规范和验证规范,但存在一些差异。See Extensions and Omissions for details.有关详细信息,请参阅扩展和省略。
For more information about JSON Schema, see the official website.有关JSON模式的更多信息,请参阅官方网站。
Keyword | |||
---|---|---|---|
bsonType | all types | string alias or array of string aliases | $type operator$type 运算符的相同字符串别名
|
enum | all types | array of values | |
type | all types | string or array of unique strings |
|
allOf | all types | array of JSON Schema objects | |
anyOf | all types | array of JSON Schema objects | |
oneOf | all types | array of JSON Schema objects | |
not | all types | a JSON Schema object | |
multipleOf | numbers | number | |
maximum | numbers | number | |
exclusiveMaximum | numbers | boolean | maximum is an exclusive maximum. true 且字段为数字,则maximum 为排除式的最大值。 |
minimum | numbers | number | |
exclusiveMinimum | numbers | boolean | minimum is an exclusive minimum. minimum 为排除式的最小值。 |
maxLength | strings | integer | |
minLength | strings | integer | |
pattern | strings | string containing a regex | |
maxProperties | objects | integer | |
minProperties | objects | integer | |
required | objects | array of unique strings | |
additionalProperties | objects | boolean or object |
|
properties | objects | object | |
patternProperties | objects | object | properties requirements, each property name of this object must be a valid regular expressionproperties 要求外,此对象的每个属性名必须是有效的正则表达式 |
dependencies | objects | object | |
additionalItems | arrays | boolean or object | |
items | arrays | object or array | |
maxItems | arrays | integer | |
minItems | arrays | integer | |
uniqueItems | arrays | boolean | true ,则数组中的每个项都必须是唯一的。 |
title | N/A | string | |
description | N/A | string | description field is specified, MongoDB includes the description in the error output when a document fails validation.description 字段,则当文档验证失败时,MongoDB会在错误输出中包含description 。
|
MongoDB's implementation of JSON Schema includes the addition of the MongoDB对JSON模式的实现包括添加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.$type
运算符的相同字符串别名。
The following are not supported in MongoDB's implementation of JSON Schema:MongoDB对JSON模式的实现不支持以下内容:
The keywords:关键词:
$ref
$schema
default
definitions
format
id
integer
type. integer
类型。int
or long
with the bsonType
keyword.int
或long
与bsonType
关键字一起使用。