Firestore删除文档及其引用的所有文档 [英] Firestore delete document and all documents referencing it
问题描述
这个问题与Firebase(+ Flutter)有关
this question is regarding Firebase (+ Flutter)
是否可以删除文档以及引用该文档的所有文档?
is it possible to delete a document and all documents referencing it?
假设我具有这样的结构:
Lets assume i have a structure like this:
-users (collection)
- name
- bookmarks ((sub)collection)
- postId
-post (collection)
- userId
- name
- ...
现在,如果我删除帖子,我想删除所有引用该帖子的书签。 (来自所有用户)。有自动的方法吗(来自RDBMS)。
Now if i delete a post, i want to delete all bookmarks referencing that post. (From all users). Is there an automatic way (coming from RDBMS).
另一个问题:
如何查询所有帖子并加入用户信息。
Another question: How can i query all posts and "join" the user infos.
类似这样的东西:
return firestore
.collection("posts")
.orderBy("createdAt")
.snapshots(); // I want to join the userInformation
这些嵌套查询是否等于api限制?
And are these nested queries then and counting to the api limit?
预先感谢
推荐答案
您必须查询文档并然后批量删除它们。我不使用子集合,也不打算查询这些子集合,但是使用根级别架构,您可以删除带有标准Firestore删除的帖子,并引用documentID。假设documentID与您的 postID字段相同(如果不是,则重构您的架构),然后:
you have to query the docs and then batch delete them. i don't use subcollections, and i'm not going to look up how to query them, but with a root level schema you'd delete the post with a standard firestore delete, referencing the documentID. assuming the documentID is the same as your 'postID' field (and if it isn't, refactor your schema) you'd then:
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次批处理:
be aware that batches can only be done 500 at a time:
https://firebase.google.com/docs / firestore / manage-data / transactions
所以您必须拆分 _batch.commit()
必要时调用。
so you'd have to split up your _batch.commit()
calls as necessary.
这篇关于Firestore删除文档及其引用的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!