A migration transforms an existing realm and its objects from its current Realm Schema version to a newer one. Application data models typically change over time to accommodate new requirements and features. Migrations give you the flexibility to automatically update your existing application data whenever a client application upgrades to a newer version.

Key Concepts

Local Migration

A local migration is a migration for a realm that does not automatically Sync with another realm. Local migrations have access to the existing Realm Schema, version, and objects and define logic that incrementally updates the realm to its new schema version. To perform a local migration you must specify a new schema version that is higher than the current version and provide a migration function when you open the out-of-date realm.

MongoDB Realm automatically migrates certain changes, such as new and deleted properties, but does not automatically set values for new properties unless the updated object schema specifies a default value. You can define additional logic in the migration that dynamically calculates a new property’s value. Migrations do not allow you to directly rename a property. Instead, it treats a renamed property as a new property that you must set to the value of the old property before MongoDB Realm automatically deletes it.


A realm using schema version 1 has a Person object type:

public class Person : RealmObject
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

The developer decides that the Person class should use a combined fullName field instead of the separate firstName and lastName fields:

public class Person : RealmObject
    public string FullName { get; set; }
    public int Age { get; set; }

To migrate the realm to conform to the updated Person schema, the developer sets the realm’s schema version to 2 and defines a migration function to set the value of fullName based on the existing firstName and lastName properties:

var config = new RealmConfiguration
    SchemaVersion = 2,
    MigrationCallback = (migration, oldSchemaVersion) =>
        var oldPeople = migration.OldRealm.All("Person");
        var newPeople = migration.NewRealm.All("Person");

        // Migrate Person objects
        for (var i = 0; i < newPeople.Count(); i++)
            var oldPerson = oldPeople.ElementAt(i);
            var newPerson = newPeople.ElementAt(i);
            newPerson.FullName = oldPerson.FirstName + " " + oldPerson.LastName;

var realm = Realm.GetInstance(config);

Synced Migration

A synced migration is a migration for a realm that automatically Syncs with another remote realm. Realm Database automatically handles all synced schema migrations and does not allow you to specify a migration function.

Synced realms represent multiple end users and devices that will likely not all immediately update to the most recent version of an application. MongoDB Realm ensures that synced schema changes are backwards compatible, which means that client applications that have not updated to the most recent version can still sync with clients that are using the most recent version.

MongoDB Realm handles synced migrations using the following framework:

Change Type Description
Schema Version MongoDB Realm ignores any schema version set by the client application.
Migration Function MongoDB Realm throws an error if the realm configuration includes a local migration function.
Additions MongoDB Realm automatically applies additive changes, such as a new class or class property.

MongoDB Realm does not delete removed fields from the database, but ignores them in future read operations. MongoDB Realm includes deleted properties in new objects with a default value that depends on whether or not the property was required:

  • MongoDB Realm sets the value of optional deleted properties to null.
  • MongoDB Realm sets the value of required deleted properties to the empty value of the property type (e.g. 0 for numeric fields, "" for string properties, etc.).

MongoDB Realm prevents modifications to an existing property other than delete operations. Modifications that synced realms do not support include:

  • Changing a property’s type but keeping the same name.
  • Changing an object type’s primary key.
  • Changing a property from optional to required (or vice-versa).


  • A migration transforms an existing realm and its objects from its current schema version to a later one.
  • MongoDB Realm allows you to specify migration functions for local migrations, i.e. migrations where the realm is not synced with MongoDB Realm.
  • MongoDB Realm automatically handles synced migration, i.e. migrations where the realm is synced. MongoDB Realm does not allow migration functions for such migrations.
←   Collections Client Resets  →