- Stitch >
- MongoDB Atlas >
- Reference
Document Schemas¶
On this page
Overview¶
A MongoDB service document schema is a JSON object that allows you to define the shape and content of documents and embedded documents in a collection. You can use a schema to require a specific set of fields, configure the content of a field, or to validate changes to a document based on its beginning and ending states.
Document schemas follow the same JSON schema specification as the
document validation built in to
the MongoDB server. A schema can represent any of the BSON
types
supported by the $type
operator. This page describes schemas for the
following common types:
Schema Enforcement Process¶
Stitch evaluates the result of all document writes (inserts and updates) and compares them against the schema before committing the writes to your cluster. If the result of a write operation does not match the schema, Stitch will roll back the write operation and return an error to the user.
Example
A collection has the following document schema:
A user with permission to read and write all fields wants to update
the name
field of a particular document. They issue the following
query:
The query attempts to set the value of name
to the number 42
,
but the schema requires the value to be a string
. Stitch will
reject this write operation even though the user had permission to
update the document because the write result does not conform to the
schema.
Document Schema Configuration¶
Documents in MongoDB are objects stored in a format called BSON, a binary-encoded superset of JSON that supports additional data types. The root of every document schema in Stitch is a BSON Object schema that applies to each document in a collection.
Schema Data Types¶
General¶
The following fields are available for all schema types:
Field Name | Description |
---|---|
type |
The JSON type of the property the schema describes. If the
property’s value can be of multiple types, specify an array of
JSON types. Cannot be used with the The following standard JSON types are available:
Note MongoDB’s JSON Schema implementation does not support the
|
bsonType |
The BSON type of the property the schema describes. If the
property’s value can be of multiple types, specify an array of
BSON types. Cannot be used with the BSON types include all JSON types as well as additional types that you can reference by their string alias, such as:
|
enum |
An array that includes all valid values for the data that the schema describes. |
title |
A short title or name for the data that the schema models. This field is used for metadata purposes only and has no impact on schema validation. |
description |
A detailed description of the data that the schema models. This field is used for metadata purposes only and has no impact on schema validation. |
Objects¶
The object
schema type configures the content of documents. The
following fields are available for object
schemas:
For more information, see the official JSON Schema object guide.
Field Name | Description |
---|---|
required |
An array of field names that must be included in the document. |
title |
A type name for the object. Stitch uses this value to name the document’s type in the GraphQL API. |
properties |
An object where each field maps to a field in the parent object by name. The value of each field is a schema document that configures the value of the field. |
minProperties |
The minimum number of fields allowed in the object. |
maxProperties |
The maximum number of fields allowed in the object. |
patternProperties |
An object where each field is a regular expression string that maps to all fields in the parent object that match. The value of each field is a schema document that configures the value of matched fields. |
additionalProperties |
Default: If If the value is a schema object, any additional fields must validate against the schema. |
dependencies |
Specify property and schema dependencies. |
Arrays¶
The array
schema type configures the content of array fields. The
following fields are available for array
schemas:
For more information, see the official JSON Schema array guide.
Field Name | Description |
---|---|
items |
A schema for all array items, or an array of schemas where order matters. |
additionalItems |
Default: If If the value is a schema object, any additional fields must validate against the schema. Note The |
maxItems |
The maximum length of the array. |
minItems |
The minimum length of the array. |
uniqueItems |
Default: If |
Strings¶
The string
schema type configures the value of string fields. The
following fields are available for string
schemas:
For more information, see the official JSON Schema string guide.
Field Name | Description |
---|---|
maxLength |
The maximum number of characters in the string. |
minLength |
The minimum number of characters in the string. |
pattern |
A regular expression string that must match the string value. |
Numbers¶
The numeric
schema type configures the content of numeric fields,
such as integers and decimals. The following fields are available for
numeric
schemas:
For more information, see the official JSON Schema numeric guide.
Field Name | Description |
---|---|
multipleOf |
An integer divisor of the field value. For example, if
multipleOf is set to 3 , 6 is a valid value but
7 is not. |
maximum |
The maximum value of the number. |
exclusiveMaximum |
Default: If |
minimum |
The minimum value of the number. |
exclusiveMinimum |
Default: If |
Booleans¶
The boolean
schema type configures the content of fields that are
either true
or false
.
For more information, see the official JSON Schema boolean guide.