Does mongo realm sync any model that has relation with a synced model?

Let’s say we have this model:

class Child{
   String partitionKey = "child";
}
class Parent{
   String partitionKey = "parent";
   Child child;
}

If we choose partitionKey as our partition key and try sync partition “parent” without syncing “child”. Does realm sync child when syncing “parent”?

Not sure which SDK you’re using, but if I were working with Swift then I’d declare the classes like this:

import RealmSwift

@objcMembers class Parent: Object, ObjectKeyIdentifiable {
    dynamic var _id = UUID().uuidString
    dynamic var child: Child?
}

@objcMembers class Child: EmbeddedObject, ObjectKeyIdentifiable {
    dynamic var _id = UUID().uuidString
}

Note that there’s no need to add partitionKey to Parent as the SDK will handle that.

As Child is embedded in Parent, it will be stored as an embedded document within Parent docs in Atlas.

When you open a Realm, you provide the partition you want to work with – it will then sync all documents from the Parent collection where partitionKey matches the string you specify for the partition (including the Child data.

You can read a lot more about how Realm partitions work in this article.

Hey @Andrew_Morgan
Thanks for reply. I have read your article about strategies. The structure that I added in my question is just a sample. I know there is better strategies for this sample but think it as part of a bigger structure that you have to use this way.
Maybe adding structure made my question complex but it’s a simple question.
if model A has a reference to model B but they are in different portion, Does mongo sync model B during syncing model A?

What I described was embedding (where on the Atlas side, Child documents are actually sub-documents within the Parent collection.

The other way to model this is to define relationships between the Parent and Child collections in the backend Realm schema. In this case, the Child will only get synced if its partition is set to the same value as the Parent document – otherwise, child will be set to nil.