How can I update more than 500 docs in Firestore using Batch?

后端 未结 3 1779
说谎
说谎 2020-12-03 05:56

I\'m trying to update a field timestamp with the Firestore admin timestamp in a collection with more than 500 docs.

const batch = d         


        
3条回答
  •  盖世英雄少女心
    2020-12-03 06:26

    I liked this simple solution using typescript:

    const users = await db.collection('users').get()
    
    const batches = _.chunk(users.docs, 500).map(userDocs => {
        const batch = db.batch()
        userDocs.forEach(doc => {
            batch.set(doc.ref, { field: 'myNewValue' }, { merge: true })
        })
        return batch.commit()
    })
    
    await Promise.all(batches)
    

    Just remember to add import * as _ from "lodash" at the top. Based on this answer.

提交回复
热议问题