最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Firestore删除子集合中的所有文档

SEO心得admin58浏览0评论
本文介绍了Firestore删除子集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的Cloud Firestore设置(iOS/Swift应用程序)中,我有一个Post集合和一个Likes子集合. Likes子集合具有一个文档,其中包含喜欢该帖子的用户的信息,而documentID是喜欢者的用户ID.

In my Cloud Firestore setup (iOS/Swift app), I have a Post collection with a Likes subcollection. The Likes subcollection has a document that contains the information of the user that liked that post, with the documentID being the userID of the liker.

当用户更新其帖子时,我想清除Likes子集合(在我的应用中,该用户仅更新了一个帖子,而不是创建一个新帖子).

When the user updates their post, I want to clear the Likes subcollection (in my app, the user just updates a post instead of creating a new one).

我已经读过,不建议删除子集合中的每个文档,所以我想知道如何才能做到这一点.受欢迎的用户可能有成千上万的喜欢,因此全部删除它们可能会耗资巨大.

I have read that deleting every document in a subcollection is not advised, so I am wondering how I can accomplish this. A popular user could have thousands and thousands of likes, so deleting them all could be costly.

据我了解,我可以使用Cloud Functions以某种方式完成此任务吗?我尝试阅读有关Cloud Functions的文章,但我正迷路了.有没有一种方法可以触发Cloud Function在更新其用户的帖子后删除该用户的Likes子集合中的所有文档?

It's my understanding that I can accomplish this somehow using Cloud Functions? I tried reading up on Cloud Functions and I'm getting pretty lost. Is there a way I can trigger a Cloud Function to delete all the documents in the Likes subcollection for that user upon updating their post?

推荐答案

在Cloud Functions中,您只是使用Firebase Admin SDK来访问Firestore.在Cloud Functions中运行该SDK不会获得任何神奇的额外API.

From Cloud Functions you're just using the Firebase Admin SDK to access Firestore. There is no magic extra APIs that you get from running that SDK in Cloud Functions.

无论是在客户端SDK还是在Admin SDK中,都无法一次性删除集合中的所有文档.有关更多信息,请参见:

There is no way to delete all documents from a collection in one go, neither in the client-side SDKs nor in the Admin SDK. For more on this, see:

  • 删除Firestore集合中的所有文档
  • 为什么单个散装"
  • Deleting all documents in Firestore collection
  • Why "single bulk" delete collection in Cloud Firestore is not possible like it is with Realtime Database? (which also explains the reason)
  • Delete firestore collection (which points to using Cloud Functions as a custom API endpoint)
发布评论

评论列表(0)

  1. 暂无评论