Dani
Dani

Reputation: 4177

Update all documents in Firestore

I have made a change in my documents, one string field now is an array and the name also changed. So it went from:

phoneNumber: "+34616161616"

to:

phone: {
    phoneNumber: "616161616",
    phoneCode: "+34",
    isoCode: "ES"
}

After doing the refactor in my app I was supporting also the old version but I realised that I'd need to do many changes.

Can I updated instead all documents in Firestore? Is there any way inside Firebase to update this at once?

The structure is:

1- Users (collection) --> userId --> portfolios (collection) --> portfolioId --> rents (collection) --> collectionId --> phone

2- Users (collection) --> userId --> contacts (collection) --> contactId --> phone

Upvotes: 0

Views: 468

Answers (1)

Echo
Echo

Reputation: 7

functions.firestore.document('/accounts/{accountId}/resources/{resourceId}')

.onUpdate((change, context) => { const resource = context.params.resourceId;

return admin.firestore().collection('/accounts/'+account+'/tasks')
  .where('resourceId', '=', resource).get().then(snapshot => {
    const promises = [];
    snapshot.forEach(doc => {
      promises.push(doc.ref.update({
        fieldA: 'valueA',
        fieldB: 'valueB'
      }));
    });
    return Promise.all(promises)
})
.catch(error => {
  console.log(error);
  return null;
});

}); this should work to update your documents

Upvotes: 1

Related Questions