Navigation

Migrations

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.

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.

Beaker IconExample

Note: assume that each schema change in this example occurs after the developer has used each version for some amount of time. New schema version numbers only apply once you open the Realm with an updated schema and explicitly specify the new version number, so in order to get to version 4, you would first need to open the app with versions 2 and 3.

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

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

First, the developer decides to add a "LastName" property:

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; }
}

Lastly, the developer decides to modify the Age property by renaming it to Birthday and changing the type to DateTimeOffset:

public class Person : RealmObject
{
public string FullName { get; set; }
public DateTimeOffset Birthday { get; set; }
}

To migrate the realm to conform to the updated Person schema, the developer sets the realm's schema version to 4 and defines a migration function to set the value of FullName based on the existing FirstName and LastName properties and the value of Birthday based on Age:

var config = new RealmConfiguration
{
SchemaVersion = 4,
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);
// Changes from version 1 to 2 (adding LastName) will occur automatically when Realm detects the change
// Migrate Person from version 2 to 3: replace FirstName and LastName with FullName
// LastName doesn't exist in version 1
if (oldSchemaVersion < 2)
{
newPerson.FullName = oldPerson.FirstName;
}
else if (oldSchemaVersion < 3)
{
newPerson.FullName = $"{oldPerson.FirstName} {oldPerson.LastName}";
}
// Migrate Person from version 3 to 4: replace Age with Birthday
if (oldSchemaVersion < 4)
{
var birthYear = DateTimeOffset.UtcNow.Year - oldPerson.Age;
newPerson.Birthday = new DataTimeOffset(birthYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
}
}
}
};
var realm = Realm.GetInstance(config);

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 TypeDescription
Schema VersionMongoDB Realm ignores any schema version set by the client application.
Migration FunctionMongoDB Realm throws an error if the realm configuration includes a local migration function.
AdditionsMongoDB Realm automatically applies additive changes, such as a new class or class property.
Deletions

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.).
Modifications

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.
Give Feedback