Firestore perform delete based on condition

后端 未结 2 387
余生分开走
余生分开走 2021-01-13 01:38

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

I see the delete function

相关标签:
2条回答
  • 2021-01-13 01:57

    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());
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-13 02:10

    Here's my method for both querying and deleting documents from firestore. First it queries the data, then it deletes it. Note, this method must be adapted for integer/double values.

        public void whereQueryDelete(final String collection, final String field, final String value) {
        mFirestoreDatabase.collection(collection)
                .whereEqualTo(field, value)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                                String idDelete = document.getId();
                                mFirestoreDatabase.collection(collection).document(idDelete)
                                        .delete()
                                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void aVoid) {
                                                Log.d(TAG, "DocumentSnapshot successfully deleted!");
                                            }
                                        })
                                        .addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Log.w(TAG, "Error deleting document", e);
                                            }
                                        });
                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });
    }
    
    0 讨论(0)
提交回复
热议问题