如何删除包含所有集合和嵌套子集合的文档?(在函数环境中)
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.
创建一个超级"函数,它将抓取每个文档并批量删除它们.此函数会很复杂,不易更改,并且可能需要很长时间的执行时间.
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 文档:firebase.google/docs/firestore/solutions/delete-collections删除带有嵌套子集合的集合可能会在服务器端使用 node-JS 轻松整洁地完成.
according to firebase documentation: firebase.google/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 });