- Reference >
- Operators >
- Query and Projection Operators >
- Evaluation Query Operators >
- $jsonSchema
$jsonSchema¶
On this page
Definition¶
-
$jsonSchema
¶ New in version 3.6.
The
$jsonSchema
operator matches documents that satisfy the specified JSON Schema.The
$jsonSchema
operator expression has the following syntax:Where the JSON Schema object is formatted according to draft 4 of the JSON Schema standard.
For example:
For a list of keywords supported by MongoDB, see Available Keywords.
Note
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences. See Extensions and Omissions for details.
For more information about JSON Schema, see the official website.
Behavior¶
Feature Compatibility¶
The featureCompatibilityVersion must be set to
"3.6"
or higher in order to use $jsonSchema
.
Document Validator¶
You can use $jsonSchema
in a document validator to enforce the
specified schema on insert and update operations:
Query Conditions¶
You can use $jsonSchema
in query conditions for read and write
operations to find documents in the collection that satisfy the specified
schema:
To find documents in the collection that do not satisfy the specified
schema, use the $jsonSchema
expression in a $nor
expression. For example:
Examples¶
Schema Validation¶
The following db.createCollection()
method creates a
collection named students
and uses the $jsonSchema
operator to set schema validation rules:
Given the created validator
for the collection, the following insert
operation will fail because gpa
is an integer when the validator
requires a double
.
The operation returns the following error:
After changing the gpa
to a double, the insert succeeds:
The operation returns the following:
Query Conditions¶
You can use $jsonSchema
in query conditions for read and write
operations to find documents in the collection that satisfy the specified
schema.
For example, create a sample collection inventory
with the
following documents:
Next, define the following sample schema object:
You can use $jsonSchema
to find all documents in the
collection that satisfy the schema:
You can use $jsonSchema
with the $nor
to find all
documents that do not satisfy the schema:
Or, you can update all documents that do not satisfy the schema:
Or, you can delete all documents that do not satisfy the schema:
JSON Schema¶
MongoDB supports draft 4 of JSON Schema, including core specification and validation specification, with some differences. See Extensions and Omissions for details.
For more information about JSON Schema, see the official website.
Available Keywords¶
Note
MongoDB implements a subset of keywords available in JSON Schema. For a complete list of omissions, see Omissions.
Keyword | Type | Definition | Behavior |
---|---|---|---|
bsonType | all types | string alias or array of string aliases | Accepts same string aliases
used for the $type operator |
enum | all types | array of values | Enumerates all possible values of the field |
type | all 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”. MongoDB’s implementation of the JSON Schema does not support the
“integer” type. Use the |
allOf | all types | array of JSON Schema objects | Field must match all specified schemas |
anyOf | all types | array of JSON Schema objects | Field must match at least one of the specified schemas |
oneOf | all types | array of JSON Schema objects | Field must match exactly one of the specified schemas |
not | all types | a JSON Schema object | Field must not match the schema |
multipleOf | numbers | number | Field must be a multiple of this value |
maximum | numbers | number | Indicates the maximum value of the field |
exclusiveMaximum | numbers | boolean | If true and field is a number, maximum is an exclusive maximum.
Otherwise, it is an inclusive maximum. |
minimum | numbers | number | Indicates the minimum value of the field |
exclusiveMinimum | numbers | boolean | If true, minimum is an exclusive minimum. Otherwise, it is an
inclusive minimum. |
maxLength | strings | integer | Indicates the maximum length of the field |
minLength | strings | integer | Indicates the minimum length of the field |
pattern | strings | string containing a regex | Field must match the regular expression |
maxProperties | objects | integer | Indicates the field’s maximum number of properties |
minProperties | objects | integer | Indicates the field’s minimum number of properties |
required | objects | array of unique strings | Object’s property set must contain all the specified elements in the array |
additionalProperties | objects | boolean or object | If Defaults to |
properties | objects | object | A valid JSON Schema where each value is also a valid JSON Schema object |
patternProperties | objects | object | In addition to properties requirements, each property name of this
object must be a valid regular expression |
dependencies | objects | object | Describes field or schema dependencies |
additionalItems | arrays | boolean or object | If an object, must be a valid JSON Schema |
items | arrays | object or array | Must be either a valid JSON Schema, or an array of valid JSON Schemas |
maxItems | arrays | integer | Indicates the maximum length of array |
minItems | arrays | integer | Indicates the minimum length of array |
uniqueItems | arrays | boolean | If true, each item in the array must be unique. Otherwise, no uniqueness constraint is enforced. |
title | N/A | string | A descriptive title string with no effect. |
description | N/A | string | A string that describes the schema and has no effect. |
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.
Omissions¶
The following are not supported in MongoDB’s implementation of JSON Schema:
- Hypertext definitions in draft 4 of the JSON Schema spec.
- The keywords:
$ref
$schema
default
definitions
format
id
- The
integer
type. You must use the BSON typeint
orlong
with thebsonType
keyword. - Hypermedia and linking properties of JSON Schema, including the use of JSON References and JSON Pointers.
- Unknown keywords.