如何从 Firestore 中删除集合或子集合? [英] How delete a collection or subcollection from Firestore?

查看:40
本文介绍了如何从 Firestore 中删除集合或子集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为lists的集合,它有ID代表列表ID的文档.该文档有一个名为 employees 的集合和另一个名为 locations 的集合.

I have a collection called lists and it has documents who is ID represents the list ID. This document has a collection called employees and another one called locations.

结构如下:

(lists)
    -listId
       (employees)
       (locations)

如果用户想要删除特定列表,那么问题是我们无法删除 listId,因为这将保留集合(如 Firestore 文档所述).

If the user wants to delete a specific list then the problem is that we can't delete listId because that will keep the collection (as was mentioned by Firestore docs).

如何对结构进行建模以满足需求?我似乎无法绕过子集合的需要.

How can the structure be modeled to fit the needs? I can't seem to get around the need for subcollection.

有什么推荐吗?

推荐答案

无需为了删除某些集合而重构数据库.要删除 Cloud Firestore 中的整个集合或子集合,请检索集合或子集合中的所有文档并将其删除.因此,要删除特定列表,请使用以下步骤:

There is no need to restructure your database in order to delete some collections. To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. So in order to delete a specific list, please use the following steps:

  1. 找到employees集合下的所有文档并删除它们
  2. 找到locations集合下的所有文档并删除它们
  3. 删除listId文档
  1. Find all documents beneath employees collection and delete them
  2. Find all documents beneath locations collection and delete them
  3. Delete the listId document

如果您有较大的集合,您可能希望以较小的批次删除文档以避免内存不足错误.重复该过程,直到您删除了整个集合或子集合.

If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors. Repeat the process until you've deleted the entire collection or subcollection.

即使 Firebase 团队不推荐删除操作,因为它对安全和性能有负面影响,您仍然可以这样做,但仅限于小型集合.如果您需要删除整个网络收藏集,请仅从受信任的服务器环境中执行此操作.

Even if the delete operation is not recomended by Firebase team because it has negative security and performance implications, you can still do it but only for small collections. If you need to delete entire collections for web, do so only from a trusted server environment.

对于Android,您可以使用以下代码:

For Android, you can use the following code:

private void deleteCollection(final CollectionReference collection, Executor executor) {
    Tasks.call(executor, () -> {
        int batchSize = 10;
        Query query = collection.orderBy(FieldPath.documentId()).limit(batchSize);
        List<DocumentSnapshot> deleted = deleteQueryBatch(query);

        while (deleted.size() >= batchSize) {
            DocumentSnapshot last = deleted.get(deleted.size() - 1);
            query = collection.orderBy(FieldPath.documentId()).startAfter(last.getId()).limit(batchSize);

            deleted = deleteQueryBatch(query);
        }

        return null;
    });
}

@WorkerThread
private List<DocumentSnapshot> deleteQueryBatch(final Query query) throws Exception {
    QuerySnapshot querySnapshot = Tasks.await(query.get());

    WriteBatch batch = query.getFirestore().batch();
    for (DocumentSnapshot snapshot : querySnapshot) {
        batch.delete(snapshot.getReference());
    }
    Tasks.await(batch.commit());

    return querySnapshot.getDocuments();
}

这篇关于如何从 Firestore 中删除集合或子集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆