Docs Menu
Docs Home
/ /
Atlas App Services
/ /

Create a Data Model

On this page

  • Overview
  • Prerequisites
  • Create an App Services Schema from a Realm Object Model
  • Create a Realm Object Schema from an App Services Schema
  • Further Reading

The Device Sync data model consists of two consistent schemas: the Realm Object Schema used in Atlas Device SDK and the App Services Schema.

You can create the data model for your App through the Realm Object Schema first or through the App Services Schema first:

  • Create an App Services Schema from a Realm Object Schema: If you are developing mobile-first and do not already have data in your Atlas cluster, you can translate your Realm Object Schema into an App Services Schema.

  • Create a Realm Object Schema from an App Services Schema: If you have data in your MongoDB Atlas cluster already, MongoDB generates a schema by sampling your data. Atlas App Services can then translate that schema into a Realm Object Schema to use in your mobile application with the Atlas Device SDK.

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 automatically updated.

Your App must have at least one linked data source in order to create a schema.

However, it cannot be a serverless instance or Federated database instance.

You can alter or define a Realm Object Schema through your mobile client SDK. Changes to your Realm Object Schema are only allowed when Development Mode is enabled. App Services will reflect any changes to your Realm Object Schema in your App Services Schema.

Refer to the Atlas Device SDK-specific documentation for creating Realm Object Schemas.

1

Development Mode is set in the App Services UI.

To enable Development Mode, click the slider to the right of Development Mode.

The UI to enable Development Mode
click to enlarge
2

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 Schema in your client code. Data Validation occurs when Development Mode is off, so App Services does not accept changes to your Realm Object Schema while Development Mode is not on.

Important

Primary Key _id Required

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

Example

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 App Services UI and then edit their user model within their client code.

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

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

In the Sync screen, turn off Development Mode by clicking the slider next to Development Mode. The UI indicates that Development Mode has been turned off.

The UI that shows Development Mode is disabled
click to enlarge

Note

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

1

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

Note the following requirements:

  • The App Services Schema must have a primary key field called _id. _id can be of type string, int, or objectId.

  • Your schema object type names cannot exceed 57 UTF-8 characters.

2

The Realm Object Schema defines and validates your data in your mobile client application. To view your Realm Object Schema, navigate to the Atlas Device SDKs page, then click the Realm Object Models tab. On this page, you can view your App Services Schema as a generated Realm Object Schema in your language of choice.

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

You can use the generated Realm Object Schema in your client application. In order to begin enforcing data validation with your data model, you can open a realm with the Realm Object Schema. This will prevent improper data from entering your database from your mobile client.

Click Copy on the right-hand side of the Realm Object Schema for the Object Model you want to integrate into your mobile application code. This will copy the Realm Object Schema code for the SDK of your choice into your clipboard.

Open your mobile application code in your IDE and paste the Realm Object Schema code in.

const UserSchema = { // your copied and pasted Realm Object Schema
name: 'User',
properties: {
_id: 'objectId',
_partition: 'string',
name: 'string',
birthday: 'date'
},
primaryKey: '_id'
};
// Initialize a realm with your Realm Object Schema
Realm.open({schema: UserSchema, sync: { /* ... */ }})
.then(realm => {
// ... use the realm instance to read and modify data
})

Refer to the Atlas Device SDK-specific documentation to use the generated Realm Object Schema.

Back

Sync Data Model Overview