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

编程入门 行业动态 更新时间:2024-10-09 13:22:19
本文介绍了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)

更多推荐

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

本文发布于:2023-11-30 07:48:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   Firestore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!