Cannot update a list of embedded objects as a property from another object

Hello MongoDB Community!

I hope this message finds you well. I am currently facing a challenge while working with Realm and updating properties within a list of embedded objects.

I have two object schemas, BandMember and MusicBand, with the latter containing a list of embedded BandMember objects. The issue arises when attempting to update the MusicBand object – while the name property gets updated successfully, the members property is saved as an empty array.

Given these two object schemas:

BandMember

export const BandMember: Realm.ObjectSchema = {
  name: "BandMember",
  embedded: true,
  properties: {
    _id: {
      type: "uuid",
      default: () => new Realm.BSON.UUID(Crypto.randomUUID()),
      mapTo: "id",
    },
    name: "string",
    otherEmbeddedObjectList: { type: "list", objectType: "OtherEmbeddedObject" },
  },
};

MusicBand

export const MusicBand: Realm.ObjectSchema = {
  name: "MusicBand",
  primaryKey: "_id",
  properties: {
    _id: {
      type: "uuid",
      default: () => new Realm.BSON.UUID(Crypto.randomUUID()),
    },
    name: "string",
    members: { type: "list", objectType: "BandMember" },
  },
};

Code where we do the update

realm.write(() => {
  realm.create(
    "MusicBand",
    {
      _id: 123,
      name: "Scorpions",
      members: membersArrayList,
    },
    Realm.UpdateMode.Modified,
);

I have reviewed the documentation and tried various approaches, but none seem to work for updating properties that are a list of embedded objects.

Any insights or suggestions on how to address this issue would be greatly appreciated. Thank you in advance for your help.

Just to eliminate a variable, if

is an empty array when going into the .write, then when the parent object updates the name property, it will also update the embedded list with… an empty array as that’s a .Modified situation.

Can you verify that membersArrayList contains the data you expect and is not empty before the .write?

Hi Jay, I checked if membersArrayList contains any data and it’s not empty, even hardcoding the array to be 100% sure and still updates the property with an empty array.

Maybe this - and React is not my thing but if MusicBand is the parent object which has BandMembers as embedded objects, I would expect MusicBand to be more like this

class MusicBand extends Realm.Object {
  _id!: BSON.ObjectId;
  name!: string;
  bandMembers!: Realm.List<BandMember>;

  static schema: Realm.ObjectSchema = {
    name: 'SomeName',
    properties: {
      _id: 'objectId',
      name: 'string',
      bandMembers: 'BandMember[]'; // Embed an array of objects
    },
  };
}

Again, just a guess.