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

查看:75
本文介绍了在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.

为每种Document类型添加"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.

想象一下,您将必须删除组"及其所有子级.对于#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天全站免登陆