Schema Types
On this page
All Schema Types
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 BSON types include all JSON types as well as additional types that you can reference by name:
|
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 ImportantAtlas App Services supports the The following standard JSON types are available:
NoteMongoDB's JSON Schema implementation does not support the
|
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. |
BSON Types
Array
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: If If the value is a schema object, any additional fields must validate against the schema. NoteThe |
maxItems | The maximum length of the array. |
minItems | The minimum length of the array. |
uniqueItems | Default: If |
Boolean
A bool
is either true
or false
.
{ "bsonType": "bool" }
Mixed
Note
Apps created after May 28, 2024
App Services Apps created after May 28, 2024 can store collections (arrays and dictionaries) of mixed data within a mixed data property. You can nest collections within other collections, which lets you store complex data structures such as JSON or MongoDB documents without having to define a strict data model.
To use this feature with Atlas Device SDK, you must use one of the following minimum SDK versions:
C++ SDK: version TBD
Flutter SDK: v2.0.0 or later
Kotlin SDK: v2.0.0 or later
.NET SDK: v12.2.0 or later
Node.js SDK: v12.9.0 or later
React Native SDK: v12.9.0 or later
Swift SDK: v10.51.0 or later
This feature is not supported in the Java SDK.
You can contact Support to learn more about enabling this feature in an existing app using a supported SDK.
A mixed
field may contain any schema type except for embedded objects, sets, or any SDK-supported counters. App Services does not enforce a consistent type across
documents, so two different documents may have values of different types.
Mixed fields can store arrays and dictionaries of mixed data. Sync translates these to MongoDB as arrays and objects, respectively. These collections of mixed data can also contain other collections of mixed data, with a maximum depth of 100 levels. You can leverage this flexibility to store complex data structures that wouldn't otherwise fit into a pre-defined schema, such as variable JSON data or complex MongoDB documents.
Mixed fields can also represent relationships. Sync translates these relationships to MongoDB as a DBRef to preserve the database name, collection name, and primary key of the link.
{ "bsonType": "mixed" }
Number
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: If |
minimum | The minimum value of the number. |
exclusiveMinimum | Default: If |
Object
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. App Services uses this value to name
the document's type in the GraphQL API. (GraphQL is
deprecated. Learn More) |
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. |
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.
ObjectId
An objectId
is a 12-byte 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" }
String
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. |
UUID
A uuid
(Universal Unique Identifier) is a standardized
16-byte unique object identifier.
{ "bsonType": "uuid" }
Binary Data
A binData
is a piece of unstructured binary data. Maps to the binary
BSON type. Always uses subtype 0.
{ "bsonType": "binData" }
Realm Database Types
Realm Database types are specific to Atlas Device SDK.
For more information on SDK-specific types and how to define them, refer to the SDK documentation:
Counter
A counter is a special numerical type whose value can be incremented or
decremented. It maps to the long
number type in
App Services.
{ "bsonType": "long" }
Counter data types are only supported in the following SDKs:
Set
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" } }
Dictionary
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.
Dictionary of a BSON Type
To store a dictionary with values of a BSON type, set additionalProperties
to the type's schema.
{ "bsonType": "object", "additionalProperties": { "bsonType": "string" } } }
Dictionary of Mixed BSON Types
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" } } }
Dictionary of Embedded Objects
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" } } } }
Geospatial Data
Geospatial data describes points and other data on the earth's surface. App Services does not have built-in geospatial types. Instead, you model geographic data using standard GeoJSON objects.
GeoJSON Point
A GeoJSON Point (GeoPoint) is a single location on the Earth's surface. The GeoPoint
schema must be a required type
field, of type string
, which is always set
to "Point".
You must also provide a coordinates
field, which is an array of doubles.
The coordinates
array must contain at least 2 double values, and may
contain a third:
The first double is the longitude of the point, between -180 and 180.
The second is the latitude, between -90 and 90.
The optional third value represents the elevation/altitude of the point, in meters. Atlas ignores this value when performing queries.
Note
You cannot set an array as required in a Device Sync schema, so the Sync server checks that the coordinates field is present and meets the requirements.
If you are not using Device Sync, you can query your geospatial data with regular geospatial operators. However, if you are using Atlas Device Sync, the following rules also apply to the GeoPoint object:
It must be of type
object
.It must be embedded within another type.
Additional values are allowed within the
coordinates
array, as long as they are doubles.Additional properties in the GeoPoint object are also allowed.
The following is the Device Sync schema of an object with an embedded GeoPoint property named "location":
{ "title": "MyObject", "properties": { "_id": { "bsonType": "objectId" }, "location": { "bsonType": "object", "required": [ "type" ], "properties": { "type": { "bsonType": "string", }, "coordinates": { "bsonType": "array", "items": { "bsonType": "double" } } } } } }
The following code block shows a document that follows the schema. It has a single embedded GeoPoint object named "location".
{ "_id": { "$oid": "65039d09fe4e46dddee31a3f" }, "location": { "type": "Point", "coordinates": [-122.4, 48.12, 23.0] } }