Navigation

Document Schema Types

Overview

MongoDB document schemas allow you to flexibly control the shape and content of documents and embedded documents in a collection. Schemas represent different types of data, such as strings, numbers, arrays, and document objects.

Each type of schema exposes a specific set of fields that allow you to configure the data it represents. This page lists the details of the configuration fields for each schema type.

Schema Types

General

The following fields are available for all schema types:

{
  "type": "<JSON Type>",
  "bsonType": "<BSON Type>",
  "enum": [<Value 1>, <Value 2>, ...],
  "description": "<Descriptive Text>,
  "title": "<Short Description>"
}
Field Name Description
type

The JSON type of the property the schema describes. Cannot be used with the bsonType field.

The following standard JSON types are available:

  • object
  • array
  • number
  • boolean
  • string
  • null

Note

MongoDB’s JSON Schema implementation does not support the integer JSON type. Instead, use the bsonType field with int or long as the value.

bsonType

The BSON type of the property the schema describes. Cannot be used with the type field.

BSON types include all JSON types as well as additional types that you can reference by their string alias, such as:

  • objectId
  • int
  • long
  • double
  • decimal
  • date
  • timestamp
  • regex
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.

{
  "bsonType": "object",
  "required": ["<Field Name>", ...],
  "properties": {
    "<Field Name>": <Schema Document>
  },
  "minProperties": <integer>,
  "maxProperties": <integer>,
  "patternProperties": {
    "<Field Name Regex>": <Schema Document>
  },
  "additionalProperties": <boolean> | <Schema Document>,
  "dependencies": {
    "<Field Name>": <Schema Document> | ["<Field Name>", ...]
  }
}
Field Name Description
required An array of field names that must be included in the document.
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: true.

If true, a document may contain additional fields that are not defined in the schema. If false, only fields that are explicitly defined in the schema may appear in a document.

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.

{
  "bsonType": "array",
  "items": <Schema Document> | [<Schema Document>, ...],
  "additionalItems": <boolean> | <Schema Document>,
  "maxItems": <integer>,
  "minItems": <integer>,
  "uniqueItems": <boolean>
}
Field Name Description
items A schema for all array items, or an array of schemas where order matters.
additionalItems

Default: true.

If true, the array may contain additional values that are not defined in the schema. If false, only values that are explicitly listed in the items array may appear in the array.

If the value is a schema object, any additional fields must validate against the schema.

Note

The additionalItems field only affects array schemas that have an array-valued items field. If the items field is a single schema object, additionalItems has no effect.

maxItems The maximum length of the array.
minItems The minimum length of the array.
uniqueItems

Default: false

If true, each item in the array must be unique. If false, multiple array items may be identical.

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.

{
  "bsonType": "string",
  "maxLength": <integer>,
  "minLength": <integer>,
  "pattern": "<Regular Expression>"
}
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.

{
  "bsonType": "integer" | "long" | "double" | "decimal",
  "multipleOf": <number>,
  "maximum": <number>,
  "exclusiveMaximum": <boolean>,
  "minimum": <number>,
  "exclusiveMinimum": <boolean>
}
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: false

If true, the field value must be strictly less than the maximum value. If false, the field value may also be equal to the maximum value.

minimum The minimum value of the number.
exclusiveMinimum

Default: false

If true, the field value must be strictly greater than the minimum value. If false, the field value may also be equal to the minimum value.