Docs Menu

Schema Types

On this page

  • All Schema Types
  • BSON Types
  • Array
  • Boolean
  • Mixed
  • Number
  • Object
  • ObjectId
  • String
  • UUID
  • Realm Database Types
  • Set
  • Dictionary
  • Dictionary of a BSON Type
  • Dictionary of Mixed BSON Types
  • Dictionary of Embedded Objects

The following fields are available for all BSON schemas regardless of type:

{
"bsonType": "<BSON Type>" | ["<BSON Type>", ...],
"type": "<JSON Type>" | ["<JSON Type>", ...],
"enum": [<Value 1>, <Value 2>, ...],
"description": "<Descriptive Text>,
"title": "<Short Description>"
}
Field Name
Description
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 type field.

BSON types include all JSON types as well as additional types that you can reference by name:

  • double
  • string
  • object
  • array
  • objectid
  • date
  • bool
  • null
  • regex
  • int
  • timestamp
  • long
  • decimal
  • uuid
  • mixed
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 bsonType.

Important

Realm supports the type field to maintain compatibility with the JSON schema standard. However, we recommend that you use the bsonType field instead. BSON types include all JSON schema types and support even more data types.

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.

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.

An array contains multiple values of a specific type. BSON array schemas use the standard JSON Schema array format.

{
"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.

Tip
Unique Arrays are Sets

To model a Set, use the array schema type with uniqueItems set to true.

A bool is either true or false.

{ "bsonType": "bool" }

A mixed field may contain any schema type except for arrays, sets, and dictionaries). Realm does not enforce a consistent type across documents, so two different documents may have values of different types.

{ "bsonType": "mixed" }

A number generically configures some type of number. BSON schemas extend JSON Schema numerics with additional types to define integers, floats, and decimals.

{
"bsonType": "number" | "int" | "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.

An object is a structured object with string keys that each have a typed value. Objects represent Realm objects and embedded objects in synced realms as well as the documents they map to in MongoDB.

{
"bsonType": "object",
"title": "<Type Name>",
"required": ["<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.
title
A type name for the object. Realm 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: 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.
Note
Model Dictionaries With the object Schema Type

To model dictionaries, use the object schema type with additionalProperties set to the object type of the values stored in the dictionary.

An objectId is a 12-bit identifier for BSON objects. ObjectId values are most commonly used as the unique _id values of documents in a MongoDB collection or objects in a synced realm.

{ "bsonType": "objectId" }

A string is text encoded as a series of characters. BSON string schemas use the standard JSON Schema string format.

{
"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.

A uuid (Universal Unique Identifier) is a standardized 16-byte unique object identifier.

{ "bsonType": "uuid" }

A set is a collection of unique values.

A set schema is an array schema where uniqueItems is set to true.

{
"bsonType": "array",
"uniqueItems": true,
"items": {
"bsonType": "long"
}
}

A dictionary is a collection of dynamic and unique string keys paired with values of a given type. A dictionary is functionally an object or document without pre-defined field names.

A dictionary schema is an object schema where properties is not defined and the value of additionalProperties is a schema for the dictionary value's type.

To store a dictionary with values of a BSON type, set additionalProperties to the type's schema.

{
"bsonType": "object",
"additionalProperties": {
"bsonType": "string"
}
}
}

To store a dictionary with mixed values, set additionalProperties to true:

{
"bsonType": "object",
"additionalProperties": true
}
}

Alternatively, you can define a full mixed schema:

{
"bsonType": "object",
"additionalProperties": {
"bsonType": "mixed"
}
}
}

To store a dictionary with embedded object values, define an object schema with the title field set to the embedded object's type name:

{
"bsonType": "object",
"additionalProperties": {
"bsonType": "object",
"title": “Address”,
"properties": {
"streetNumber": { "bsonType": "string" },
"street": { "bsonType": "string" },
"city": { "bsonType": "string" },
"province": { "bsonType": "string" },
"country": { "bsonType": "string" },
"postalCode": { "bsonType": "string" }
}
}
}
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.