Firestore update all documents in collections

后端 未结 4 762
执笔经年
执笔经年 2020-11-29 11:04

I have a firestore collections named users, each users have a generated id with a field score :

users 
    0e8X3VFL56rHBxxgkYOW
        score : 4
    3SeDjr         


        
4条回答
  •  青春惊慌失措
    2020-11-29 11:46

    Batch updates are nice but bare in mind that they are limited to 500 document updates per transaction. If this reset isn't done often maybe simplest approach is:

    async function resetScores() {
      const collection = await db
        .collection("users")
        .get()
      collection.forEach(doc=> {
        doc.ref
          .update({
            score: 0
          })
      })
    }
    

提交回复
热议问题