Issue with realm and related collection

The linked post has Java examples, but no JavaScript examples. I think without a concrete JavaScript example it will be difficult to understand how these documents are linked together. I did find a workaround and posted my answer on the stackoverflow article.

Here is that answer.
Realm objects can be mutated inside the realm.write function directly. So in this case the way I was able to connect tags with people is like this.

    const person = realm.write(() => {
        const realmPerson = new People(realm, {
          email,
          name,
          org_id: org._id,
        });
        
        person.tags = [{ _id: `${org._id}-2021` }];
        return realmPerson;
    });

And since the _id is the primary key field Realm just knows these objects are linked and now if I later call person.tags, I actually get the whole array of tags with all the object properties, not just the _id field.

2 Likes