Migrating from Legacy Realm Sync to MongoDB Realm Guide

Hey Folks,

I know a bunch of you have been looking for a guide on how to migrate from the old versions of Realm Object Server and Realm Cloud to MongoDB Realm. We’ve put together a guide here -
https://docs.realm.io/realm-legacy-migration-guide/

We would be interested to hear any and all feedback and how we can improve this doc over time.

-Ian

2 Likes

Great guide! That will be super helpful to a lot of developers.

Q: Is what’s laid out in the guide only applicable to working with Realm in the cloud? Or is it a general design pattern for even local (non-cloud) Realms? e.g. should local only apps include a _partitionKey? and should we be using ObjectId.generate() instead of UUID().uuidString?

Q:
In the Guide

Also, your new _id must be either an ObjectId, a String, or an Int.

Docs - probably just need to add “Int” assuming that’s allowable

A primary key is a String or ObjectId property that uniquely identifies an object.

and a question about primary keys:

In the Guide

The first thing you will need to do is convert your primary keys on your Realm Objects from your previous Primary Key field name to the required primary key of all MongoDB documents; which is _id

Docs

If an object type has a primary key, then all instances of that type must include the primary key

So two questions:

  1. In one case it sounds required, the other case sounds optional if an object instead of required. Is it required?

  2. It sounds like the primary key var must be named _id is that correct?

If every object is required to have _id, the docs should be updated to match

Object Customization

  • primaryKey()

Override this method to specify the name of a property to be used as the primary key.

Only properties of types String and Int can be designated as the primary key.

1 Like

Also, in the guide all asynchronous calls are not handled correctly. eg anything like this

app.login(credentials: creds) { (user, error) in

No longer works after v10.0.0-rc.2 as they now appear to return a single result object

  • All Swift callbacks for asynchronous operations which can fail are now passed a Result<Value, Error> parameter instead of two separate Value? and Error? parameters.

So it looks like this

app.login(credentials: creds) { result in

Not sure why that change was made - it broke all of my error handling. Can we have an example of how that should be properly implemented?

IMO Result is too close to the existing Results object so it should have been named something else, but that’s me. :wink:

Edit: I believe the intended implementation is something like this?

switch result {
   case .success(let realm):
      //do something with realm
   case .failure(let error):
      print(error.localizedDescription)
}
1 Like

Hey Jay, thanks for the comments. This guide is just for legacy Realm Sync users - non-sync users are not affected. The primary key _id field is only required for sync users, the docs are allowing ambiguity for non-sync users since there is no such requirement - we are working to split our docs right now to make it clear what is sync and what is non-sync. Stay tuned.

The Swift team did get a last minute change in for 10.0 that used Result in callbacks - this was to make our APIs more “swifty” since this is a pretty standard design pattern. Result is a builtin type and you don’t actually have to declare anything as a result. The Result/Results thing is pretty unfortunate, but I don’t really know what we can do about the standard library picking a name very similar to a name we were already using. We are updating the docs right now to use this new syntax and I’ve already updated the migration guide. Here is a code block for you in the meantime -

let email = "skroob@example.com"
let password = "12345"
app.login(credentials: Credentials.emailPassword(email: email, password: password)) { result in
  DispatchQueue.main.async {
   switch result {
   case .success(let realm):
      //do something with realm
   case .failure(let error):
      print(error.localizedDescription)
  }
}
2 Likes