Waseem Khan
Waseem Khan

Reputation: 50

Is there any way to update all collections at once firestore?

Currently I'm having a scenario where I need to update every single document of every collection that exists in my firestore. All I want to update all documents in all collections whenever there's any update in a specified collection. What I have seen so far is the updates of document in a single collection only.

 db.collection("cities").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        doc.ref.update({
            capital: true
        });
    });
});

This is what I can do for a single collection. Furthermore I have a generic trigger than monitors all the collections create/update/delete with wildcard of '{collectionId}/{documentId}'

functions
  .runWith({ timeoutSeconds: 540, memory: '2GB' })
  .firestore.document(`{collectionId}/{documentId}`)
  .onWrite(async (change, context) => {...}

Upvotes: 0

Views: 113

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598708

There's no built-in operation for updating all documents in all collections. But since you're running in Cloud Functions already, you can use the Node.js Admin SDK to list all collections, loop over them and then update all documents in each of them.

Upvotes: 1

Related Questions