'MongoDB Realm: How to delete element from list of embedded objects

I am using the React-Native version of MongoDB Realm. In my data model, I am using a list embedded objects. My schema looks something like this:

const EmbeddedObjectSchema = {
  name: 'EmbeddedObject',
  embedded: true,
  primaryKey: "id",
  properties: {
    id: { type: 'string' },
    amount: { type: 'int' },
  }
}

const ParentObjectSchema = {
  name: 'ParentObject',
  properties: {
    id: { type: 'string', indexed: true },
    list: { type: 'list', objectType: 'EmbeddedObject' },
  }
}

The official docs are using a very similar example (type Business contains a list of embedded Address objects). However, the docs are not clear about how you can delete an embedded object from a list directly. Based on the example in the docs, I am using the following code to delete specific list items directly:

realmDB.write(() => {
  // query for parent object
  const parentRecord = realmDB.objectForPrimaryKey(
    'ParentRecord',
    '1989023489'
  )
  // delete specific embeded objects from list
  const itemsToDelete = parentRecord.list.filtered(
    `amount == "5"`
  )
  realmDB.delete(itemsToDelete)
})

The implementation seems to work in principle, but I am insecure because I delete the embedded objects directly. In the documentation example, Contact objects are deleted, which in turn indirectly delete the nested Address objects.

So my question: Is my implementation correct, or should I not delete embedded objects directly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source