firebase - Firestore 删除文档和引用它的所有文档
问题描述
这个问题是关于 Firebase (+ Flutter)
是否可以删除文档和引用它的所有文档?
假设我有这样的结构:
-users (collection)
- name
- bookmarks ((sub)collection)
- postId
-post (collection)
- userId
- name
- ...
现在,如果我删除一个帖子,我想删除所有引用该帖子的书签。(来自所有用户)。是否有自动方式(来自 RDBMS)。
另一个问题:我如何查询所有帖子并“加入”用户信息。
像这样的东西:
return firestore
.collection("posts")
.orderBy("createdAt")
.snapshots(); // I want to join the userInformation
那么这些嵌套查询是否计入 api 限制?
提前致谢
解决方案
您必须查询文档,然后批量删除它们。我不使用子集合,也不会查找如何查询它们,但是使用根级别架构,您将使用标准的 Firestore 删除来删除帖子,并引用 documentID。假设 documentID 与您的“postID”字段相同(如果不是,请重构您的架构),您将:
final WriteBatch _batch = Firestore.instance.batch();
QuerySnapshot _query = await dbUsers.where('postId', isEqualTo: thatDocumentID).getDocuments();
_query.documents.forEach((doc) {
_batch.delete(dbUsers.document(doc.documentID));
});
await _batch.commit();
请注意,批次一次只能完成 500 个:
https://firebase.google.com/docs/firestore/manage-data/transactions
所以你必须根据_batch.commit()
需要分开你的电话。
推荐阅读
- ios - 如何在 Swift 中检测 iOS 设备是否使用 wifi 连接?
- uwp - 如何将 hololens 2 视频流式传输到 PC UWP 应用程序?
- python - DJANGO - [Errno 30] 只读文件系统:'/media'
- javascript - 弹出框没有出现在 React 中
- angular - 从接口获取值以“无法读取未定义的属性(读取'时间戳')”结尾
- swift - (Swift) ScrollView 中的 StackView 不起作用
- orientation - 不支持“Pdf”文档类型请求的 ChangeOrientation 操作 groupdocs 合并
- sql - 获得独特的匹配
- python - 将文本文件名称(类别)读入python中的不同类别字典?
- metadata - hdf5 规范中定义的属性和组 h5md 中的元数据缺少哪些属性?