How to Implement SUM() function of SQL in cloud Firestore

前端 未结 3 1993
借酒劲吻你
借酒劲吻你 2020-12-11 16:22

I am storing scores of a user in google cloud firestore as each score a new document in the collection named as \"points\".

collection name: points

3条回答
  •  既然无缘
    2020-12-11 16:49

    You can either use forEach or iterate in a for loop. This answer on stack overflow could help. Here's an example from the same:

    for (var i in querySnapshot.docs) {
        const doc = querySnapshot.docs[i]
        //do what you want to here
    }
    

    ---OR---

    you can use forEach like this

     const collRef = firebase.firestore().collection('todos');
      const query = collRef.orderBy('position');
      const items = query.get()
        .then((snapshot) => {
          let newCount = 0;
          snapshot.forEach((doc) => {
            const docRef = collRef.doc(doc.id);
            docRef.update({ position: newCount });
            newCount += 1;
          });
        });
    

提交回复
热议问题