Firestore根据条件执行删除 [英] Firestore perform delete based on condition

查看:58
本文介绍了Firestore根据条件执行删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对 field1 = x field2 = y的Firestore文档执行删除操作?

Is there a way I can perform a delete on Firestore documents where field1 =x and field2 = y?

我看到了删除功能,但是没有附带. 如果我使用事务,则存在get和delete,但get似乎不接受"where"子句.

I see the delete function but does not come with where. If I use the transaction then there is get and delete but the get does not seem to accept "where" clause.

我希望我在文档中缺少一些内容.

I hope I am missing something in the documentation.

谢谢

推荐答案

要实现此目的,您需要先创建所需的查询,然后才使用delete()方法,如下所示:

To achieve this, you need to create the desired query first and then just use the delete() method like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference itemsRef = rootRef.collection("yourCollection");
Query query = itemsRef.whereEqualTo("field1", "x").whereEqualTo("field2", "y");
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                itemsRef.document(document.getId()).delete();
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

这篇关于Firestore根据条件执行删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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