MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Migrating to using the new Embedded Object in MongoDb Realm

Hi!

I have an app that is local only for now, but I do want to get on MongoDbRealm sync.

In the old realm structure you would model a parent child relationship like this

public class Parent:Object {
let children = List
}

public class Child:Object {
private let parents:LinkingObjects = LinkingObjects(fromType: Parent.self , property: “children”)
func getParent()->Parent?{
return parent.first
}
}

But now I want the child objects to be embedded instead. How can I achieve this? Is there any example on how to migrate a non-embedded to embedded? Is there any recommended way to do this?

Right now I have simply switched the Child class to extend EmbeddedObject, but this would result in an exception on migration “Cannot convert object type ‘Child’ to embedded because objects have multiple incoming links.” How can I fix this?

Looks like I might have made this a lot harder for myself as these “child” objects are being linked to from multiple parents. This is probably where the error message is coming from.

I am guessing now that the easiest route might be to simply keep the old data in place and migrate data to new realm object using the new embedded document structure that will be better adapted to syncing.

I still think it would be nice to get some guidance here. Judging by this post https://github.com/realm/realm-java/pull/6730 I don’t think I am the only one wondering about migrating data to embedded objects.