0

我正在使用 MongoDB Realm 的 React-Native 版本。在我的数据模型中,我使用的是 list embedded objects。我的架构看起来像这样:

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' },
  }
}

官方文档使用了一个非常相似的示例(类型包含Business嵌入Address对象的列表)。但是,文档并不清楚如何直接从列表中删除嵌入对象。根据文档中的示例,我使用以下代码直接删除特定列表项:

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)
})

该实现原则上似乎可行,但我不安全,因为我直接删除了嵌入的对象。在文档示例中,Contact对象被删除,这反过来又间接删除了嵌套Address对象。

所以我的问题是:我的实现是正确的,还是我不应该直接删除嵌入的对象?

4

0 回答 0