【发布时间】:2020-12-10 08:13:26
【问题描述】:
这是 mongo 架构-
{
name: String
description: String,
array1: [{prop1: Number, prop2: String}],
referencingArray: {prop1: String, prop2_array1ref: <array1 element ref>},
}
对象类型的数组元素。 referencingArray 对象中的属性之一需要引用来自 array1 的元素。 UI 稍后将使用引用的数组元素来注释referencingArray 信息。 UI 还需要根据对array1 的更改来更新referencingArray。目前array1 对象没有任何唯一标识属性。
我的一个想法是向array1 对象添加一个guid 属性以唯一标识它们并使referencingArray 对象的prop2_array1ref 使用此guid。但这会在 array1 对象上添加一个额外的属性。
通过索引引用array1 对象需要处理从array1 中删除。如果prop2_array1ref 引用array1 的索引0 元素,则删除索引0 处的元素应该使referencingArray 处于错误状态,而不是默默地引用索引0 处的新对象。
Mongo 将 _id 属性分配给数组对象。我想过在prop2_array1ref 中使用数组对象的_id 属性,但是当数组对象还没有id 时,我不确定如何处理新条目。
问题:
从referencingArray 引用array1 元素的最佳方式是什么?
编辑以添加示例:
{
name: 'John',
description: 'Best guy',
array1: [
{prop1: 12, prop2: 'lorem'},
{prop1: 55, prop2: 'ipsum'}
],
referencingArray: {prop1: 'domur', prop2_array1ref: <array1 element 1 ref>},
// User can change prop2_array1ref to point to different element of array1 in UI.
// Question is what's a good value for prop2_array1ref?
// Options are-
// 1. Add guid to array1 elements and use that as value of prop2_array1ref. (e.g. 'fl3-42mnj-348hn-83ngns')
// 2. index of array1 elements. (e.g. 1)
// 3. mongo assigned _id of array1 elements. (e.g. 98702398420398)
}
【问题讨论】:
-
一个小样本数据集(只有 2 个文档)将有助于澄清问题。
-
@raga 添加了一个示例。