【问题标题】:Delete Nested Reference in Firestore删除 Firestore 中的嵌套引用
【发布时间】:2020-02-06 18:03:15
【问题描述】:

我正在尝试删除 Firestore 中的 2 级深度嵌套参考。我的架构如下所示:

在代码中是这样的:

{
    "folder": "bla",
    "title": "myTitle",
    "children": [
        {
            "ref": "firstReference"
        },
        {
            "ref": "secondReference"
        },
        {
            "title": "Subcollection Title",
            "children": [
                {
                    "ref": "thirdReference"
                },
                {
                    "ref": "forthReference"
                }
            ]
        }
    ]
}

现在我正在寻找一种方法来从第二个子数组中删除第三个或第四个引用。

要从第一个子数组中删除一个项目,请使用以下代码:

docRef.update({children: firebase.firestore.FieldValue.arrayRemove(folder.children[index])

但此解决方案仅适用于顶层;(

有人知道如何删除更深的嵌套元素吗?

我试过了:

docRef.update({[`children[${index}].children`]: firebase.firestore.FieldValue.arrayRemove(
                    folder.children[index].children[secondIndex])});

但它会引发错误(路径不得包含“~”、“*”、“/”、“[”或“]”)

感谢您的帮助;)

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-firestore


    【解决方案1】:

    Firestore 不支持按索引修改数组项。 FieldValue.arrayRemove 仅在您传递要从数组字段中删除的数据的确切内容 时才有效。如果你只知道索引,那么你要做的就是读取文档,修改内存中的数组,然后将新的文档内容写回。

    【讨论】:

    • 是的,我知道,但是这个folder.children[index].children[secondIndex] 是这个地方的确切内容。但是两个小时后我也想到了在内存中修改它的想法;)谢谢;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多