Docs Menu

Sync Schema Overview

On this page

  • Overview
  • Object Schema
  • Property Schema
  • Summary

In modern applications, users expect their data to be accurate no matter which device or browser they connect from. Data modeling ensures the accuracy of application data by performing type and format checks on fields.

MongoDB Realm allows you to define, synchronize, and enforce your data model in two formats:

  • A server-side BSON schema, which defines your data as MongoDB documents to enforce validation and synchronization in the cloud.
  • Realm data model, which defines your data as native objects in your mobile application.

Having these two forms of data models allows your data to be consistent and synced regardless of which clients write the data.

An object schema defines the properties and relationships of a Realm object. Object schemas are defined using JSON schema in the Schema sidebar entry of the Realm UI:

{
"title": "Dog",
"bsonType": "object",
"required": [
"_id",
"_partition",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"_partition": {
"bsonType": "string"
},
"name": {
"bsonType": "string"
},
"age": {
"bsonType": "int"
}
"breed": {
"bsonType": "string"
}
}
}
Tip
Partition field

When using Partition-Based Sync, your object schema should include a partition key. This may be a synthetic property such as the _partition field shown here. When using Flexible Sync, your schema does not need a partition key.

A property schema is a field-level configuration that defines and constrains a specific property in an object schema. Every object schema must include a property schema for each property in the object. At minimum, a property schema defines a property's data type and indicates whether the property is required.

You can configure the following constraints for a given property:

Parameter
Type
Description
Type
String

Every property in a Realm object has a strongly defined data type. A property's type can be a primitive data type or an object type defined in the same realm. The type also specifies whether the property contains a single value or a list of values.

Realm Database supports the following property types:

  • boolean
  • integer
  • double
  • string
  • date
  • decimal128
  • objectId
  • uuid
  • mixed
  • array
  • object
Tip
See also:

For more information on supported data types, see the documentation on Schema Types.

Optional
Boolean
Optional properties may contain a null value or be entirely omitted from an object. By default, all properties are optional unless explicitly marked as required.
Default
Boolean

If a client application creates a new object that does not have a value for a defined property, the object uses the default value instead.

When you attempt to create an object that is missing a value for a required field, it fails validation and does not persist to the realm.

Indexed
Boolean
A property index significantly increases the speed of certain read operations at the cost of additional overhead for write operations. Indexes are particularly useful for equality comparison, such as querying for an object based on the value of a property. However, indexes consume additional storage.
  • MongoDB Realm uses two different data models: a Realm Object Model for mobile and a Realm Schema for Atlas. Changes to one data model match the other data model.
  • If you already have data in Atlas, MongoDB Realm creates a Realm Schema by sampling that data. That Realm Schema can be translated into a Realm Object Model to be used in your Realm mobile application code.
  • If you do not have data in Atlas or are developing with a mobile-first approach, you can turn on Development Mode to allow for data model changes from a Realm mobile client. When you finish developing your Realm Object Model, you can turn off Development Mode, and your Realm Schema will be auto-updated with your updated data model configuration. Atlas will begin using this updated data model configuration for data validation on your cluster immediately.
←  Configure Your Data ModelCreate a Schema →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.