在 Firestore 中删除包含所有子集合和嵌套子集合的文档 [英] Delete a Document with all Subcollections and Nested Subcollections in Firestore

查看:49
本文介绍了在 Firestore 中删除包含所有子集合和嵌套子集合的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除包含所有集合和嵌套子集合的文档?(在函数环境中)

How can you delete a Document with all it's collections and nested subcollections? (inside the functions environment)

在 RTDB 中,您可以 ref.child('../someNode).setValue(null) 并完成所需的行为.

In the RTDB you can ref.child('../someNode).setValue(null) and that completes the desired behavior.

我可以想到两种实现所需删除行为的方法,这两种方法都有非常可怕的缺点.

I can think of two ways you could achieve the desired delete behavior, both with tremendously ghastly drawbacks.

  1. 创建一个超级"函数,它将抓取每个文档并批量删除它们.此函数会很复杂,不易更改,并且可能需要很长时间的执行时间.

  1. Create a 'Super' function that will spider every document and delete them in a batch. This function would be complicated, brittle to changes, and might take a lengthy execution time.

为每个文档类型添加onDelete"触发器,并使其删除任何直接子集合.您将对根文档调用 delete ,并且删除调用将沿树"向下传播.由于函数执行的巨大负载,这是缓慢的、可扩展的并且代价高昂.

Add 'onDelete' triggers for each Document type, and make it delete any direct subcollections. You'll call delete on the root document, and the deletion calls will propagate down the 'tree'. This is sluggish, scales atrociously and is costly due to the colossal load of function executions.

想象一下,您必须删除一个GROUP"及其所有子项.#1 会非常混乱,#2 会很贵(每个文档调用 1 个函数)

Imagine you would have to delete a 'GROUP' and all it's children. It would be deeply chaotic with #1 and pricey with #2 (1 function call per doc)

groups > GROUP > projects > PROJECT > files > FILE > assets > ASSET
                                                   > urls > URL
                                    > members > MEMBER
               > questions > QUESTION > answers > ANSWER > replies > REPLY
                                      > comments > COMMENT
               > resources > RESOURCE > submissions > SUBMISSION
                                      > requests > REQUEST

是否有更好/更受欢迎/更干净的方法来删除文档及其所有嵌套的子集合?

Is there a superior/favored/cleaner way to delete a document and all it's nested subcollections?

考虑到您可以从控制台执行此操作,这应该是可能的.

It ought to be possible considering you can do it from the console.

推荐答案

根据 firebase 文档:
https://firebase.google.com/docs/firestore/solutions/delete-collections
删除带有嵌套子集合的集合可能会在服务器端使用 node-JS 轻松整洁地完成.

according to firebase documentation:
https://firebase.google.com/docs/firestore/solutions/delete-collections
Deleting collection with nested subcollections might be done easy and neat with node-JS on the server side.

const client = require('firebase-tools');
await client.firestore
      .delete(collectionPath, {
        project: process.env.GCLOUD_PROJECT,
        recursive: true,
        yes: true
      }); 

这篇关于在 Firestore 中删除包含所有子集合和嵌套子集合的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆