Updating a string field in all documents found in a collection in Firestore

后端 未结 3 1675
别那么骄傲
别那么骄傲 2021-01-28 19:47

Basically, I would like to know if it is possible to update all of the documents found in a collection in Firestore. I am able to get all the documents in a list like so:

<
3条回答
  •  猫巷女王i
    2021-01-28 20:30

    In order to update all the documents in a collection first you have have to retrieve all of them in a List and then iterate that list and update them. Here is a sample code:-

    for (int k = 0; k < list.size(); k++) {
        firestore.collection("Events").document(list.get(k))
            .update("Key", value).addOnSuccessListener(new OnSuccessListener()
                {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.i("Update", "Value Updated");
    
                    }
                 })
                 .addOnFailureListener(new OnFailureListener() {
                     @Override
                     public void onFailure(Exception e) {
                         Toast.makeText(MainActivity.this, "Error In Updating Details: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                     }
        });
    }
    

提交回复
热议问题