如何删除Firestore中具有不同ID的多个文档? [英] How to delete multiple documents that have different ids in firestore?
问题描述
我搜索了多个具有不同ID的文档,我需要实施某种方法将其全部删除,因此我搜索了一下,所以我猜我应该使用批处理的方法.因此,我的数据库如下图所示:图片
I got a multiple documents with different ids, and i need to implement some method to delete them all, i searched so i guess the way i should go is to use batch. So my database is as shown in this Image
所以我能够在这样的数组列表中获取我的文档ID.
so i was able to get my documents' ids in an array list like that..
[0Y5rfMK3duHBUTN9XsO5、2Q70mSjNxkAoUMDAJ8rz等...]
[0Y5rfMK3duHBUTN9XsO5, 2Q70mSjNxkAoUMDAJ8rz, etc...]
和我的代码:
WriteBatch batch = db.batch();
DocumentReference myRef = db.collection("Collection").document(String.valueOf(idsList));
batch.delete(myRef);
batch.commit();
但这是行不通的,因此,如果错过了一点步骤,或者有其他方法可以执行,请将其记录下来.
but this doesn't work, so if there is a little missed step, or if there is any other way to perform it, it would be greatly appreciated to write it down.
推荐答案
您将必须迭代列表并分别为每个列表创建一个DocumentReference.DocumentReference只能引用单个文档,而不能引用文档列表:
You will have to iterate your list and create a DocumentReference for each one individually. A DocumentReference can only refer to a single document, not a list of documents:
WriteBatch batch = db.batch();
for (String id : idsList) {
DocumentReference ref = db.collection("Collection").document(id);
batch.delete(ref);
}
batch.commit();
这篇关于如何删除Firestore中具有不同ID的多个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!