How to create/update multiple documents at once in Firestore

后端 未结 4 1182
忘掉有多难
忘掉有多难 2020-12-01 12:00

Is it possible to store multiple documents in Firestore with only one request? With this loop it\'s possible but this would cause one save operation per item in the list.

4条回答
  •  执笔经年
    2020-12-01 12:32

    Update some properties on all documents in a collection:

    resetScore(): Promise {
      return this.usersCollectionRef.ref.get().then(resp => {
        console.log(resp.docs)
        let batch = this.afs.firestore.batch();
    
        resp.docs.forEach(userDocRef => {
          batch.update(userDocRef.ref, {'score': 0, 'leadsWithSalesWin': 0, 'leadsReported': 0});
        })
        batch.commit().catch(err => console.error(err));
      }).catch(error => console.error(error))
    }
    

提交回复
热议问题