如何为该结构建模以处理删除 [英] How to model this structure to handle delete

查看:95
本文介绍了如何为该结构建模以处理删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为列表的集合,并且有一个ID为代表列表ID的文档.该文档具有名为employees的集合和另一个名为locations的集合.

I have a collection called lists and it has document who is ID represent the list ID. This document has 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团队不推荐删除操作(因为它has negative security and performance implications),您仍然可以执行删除操作,但仅适用于小型集合.如果您需要删除整个Web集合,请仅从受信任的服务器环境中删除.

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();
}

这篇关于如何为该结构建模以处理删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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