Docs Menu

Configure Your Data Model

On this page

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:

  • Realm 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": [
"properties": {
"_id": {
"bsonType": "objectId"
"_partition": {
"bsonType": "string"
"name": {
"bsonType": "string"
"age": {
"bsonType": "int"
"breed": {
"bsonType": "string"

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:


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
See also:

For more information on supported data types, see the documentation on Document Schemas.

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

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.

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.

There are two alternative approaches to configuring both data models:

  • Create a Realm Object Model from a Realm Schema: If you have data in your MongoDB Atlas cluster already, MongoDB generates a Realm Schema by sampling your data. MongoDB Realm can then translate that Realm Schema into a Realm Object Model to use in your mobile application.
  • Create a Realm Schema from a Realm Object Model: Alternatively, if you are developing mobile-first and do not already have data in your Atlas cluster, you can translate your Realm Object Model into a Realm Schema for use with Atlas. Regardless of the approach that you take, when you configure both your Atlas cluster and Mobile application to use the respective data model, changes to the data model between the server and client are auto-updated.
Link a MongoDB Atlas Data Source

Your Realm app must have at least one linked Atlas data source in order to define a Realm Data Model.


To get started, ensure you have a Realm Schema defined. MongoDB Realm will translate this Realm Schema into a Realm Object Model to be configured and utilized in your mobile application.

Primary Key _id Required

To work with Realm Sync, your data model must have a primary key field called _id. _id can be of type string, int, or objectId.


To learn how to define a Realm Schema for at least one collection in the synced cluster, see the Enforce a Document Schema page.


The Realm Object Model defines and validates your data in your mobile client application. To view your Realm Object Model, navigate to the SDKs page, where you can view your Realm Schema as a generated Realm Object Model in your language of choice.

The generated Realm Object Models and associated warnings in the UI
click to enlarge

Realm may fail to generate some or all of your Realm Object Model based on your Realm Schema. You can view a list of the errors in your Realm Schema that prevented Realm from generating the Realm Object Model on the SDKs page of the Realm UI.

Common errors include mismatched types, and differences in the way relationships are represented in the two respective models. For each error or warning, modify your Realm Schema to fix the specified issue.

A detailed list in the UI of warnings and errors that occurred during model
click to enlarge

You can immediately use the generated Realm Object Model in your client application. In order to begin enforcing data validation with your data model, you can open a Realm with the Realm Object Model. This will prevent improper data from entering your database from your mobile client. Click Copy on the right-hand side of the Realm Object Model for the Object Model you want to integrate into your mobile application code. This will copy the Realm Object Model code for the SDK of your choice into your clipboard. Open your mobile application code in your IDE and paste the Realm Object Model code in.

Link a MongoDB Atlas Cluster

Your Realm app must have at least one linked Atlas data source in order to define a Realm Data Model.


First, enable development mode sync.

You can alter or define a Realm Object Model through your mobile client SDK. Changes to your Realm Object Model are only allowed when Development Mode is on in the MongoDB Realm UI. MongoDB Realm will reflect these changes to your Realm Object Model in your Realm Schema used for Atlas.


As you continue to develop your application, you will need to modify your data model with it to enforce different data validation rules based on those changes. While Development Mode is on, you can edit your Realm Object Model in your client code. Data Validation occurs when Development Mode is off, so MongoDB Realm does not accept changes to your Realm Object Model while Development Mode is not on.

Primary Key _id Required

To work with Realm Sync, your data model must have a primary key field called _id. _id can be of type string, int, or objectId.


A group is developing a social media application. When the group first developed their application, a user's birthday was a required field of the User's data model. However, due to privacy concerns over the amount of user data that is stored, management creates a new requirement to make the user's birthday field an optional field. Application developers turn on Development Mode in the MongoDB Realm UI and then edit their user model within their client code.

const realmObjectModel = {
name: 'User',
properties: {
_id: 'objectId',
_partition: 'string',
name: 'string',
birthday: {type: 'date', optional: true}, // developers set optional: true to adhere to the new requirement
primaryKey: '_id'
};{schema: realmObjectModel, sync: {/*...*/}})
.then(realm => {
// ... use the realm instance to read and modify data

While Development Mode is on, MongoDB Realm doesn't validate writes against your data model, allowing you to freely update your Realm Object Model. When you turn off Development Mode, MongoDB Realm automatically updates your Realm Schema and starts to enforce data validation for your Atlas cluster based on it.

Click the "Turn Dev Mode Off" button on the top banner or in the Sync screen to turn off Development Mode. Once you turn off Development Mode, the "Development Mode is OFF" modal will appear. The modal indicates that MongoDB Realm has stopped accepting new data model changes from clients. Click the "View my Realm Schema" button on the modal to view your updated Realm Schema.


To make future data model updates from your mobile client code, you can follow this procedure again.

The banner in the UI that shows development mode is enabled
click to enlarge
The modal that confirms turning off development mode in the UI
click to enlarge
  • 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.
Give Feedback
© 2021 MongoDB, Inc.


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