Aban Pakra
Aban Pakra

Reputation: 11

Is it possible to call several update query at once for Firestore via Cloud Functions?

I am trying to update several documents in Firestore through a loop. It also creates many instances of update query so it is a messy solution when I try to listen to its changes on the client-side on the mobile app on Android and iOS and web client.

for (var item in ListOfData) {
    documentReference.update({
        SupportedCity: item
    })
    .then(function() {
        console.log("Document successfully updated!");
    })
    .catch(function(error) {
        // The document probably doesn't exist.
        console.error("Error updating document: ", error);
    });
}

Thanks In Advance 👍

Upvotes: 0

Views: 40

Answers (1)

Elias Fazel
Elias Fazel

Reputation: 2113

You can use batch.firestore(). So instead of calling update, you add it to a batch.

var batch = firestore.batch();
for (var item in ListOfData) {
 var reference = firestore.collection("[Collection_Path]").doc("Document_Path");
 batch.update/*set*/(reference , {
  SupportedCity: item
 });
}
batch.commit().then(function () {
 console.log("Document Updated");
});

Upvotes: 1

Related Questions