2D数组Firestore使用ArrayUnion



我试图在firestore中创建一个2D数组,可以使用数组联合进行更新,但我正在努力找出如何做到这一点。我可以很容易地在1D数组中这样做:

db.collection('subjects').doc('exampleDoc').update({ units: firestore.FieldValue.arrayUnion({ "name": "Example Unit", "id": "123456787654323456765" }) });

产生了这样的结果:

name: 'Example Subject',
units: [
{
id: '123456787654323456765',
name: 'Example Unit'
}
]

但是我怎么能做到这一点与下面的数据作为示例文档的2D数组?

name: 'Example Subject',
units: [
{
id: '123456787654323456765',
name: 'Example Unit',
subunits: [
{
id: '234568765434567',
name: 'Example Subunit'
}
]
}
]

使用简单的arrayUnion无法做到这一点。这是因为Firestore不支持像使用命名的嵌套字段那样查找特定索引数组元素的语法。

你需要做的就是读取文档,修改内存中的字段,使其看起来像你想要的那样,然后将整个字段更新回文档。

最新更新