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

后端 未结 3 1748
说谎
说谎 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.

    0 讨论(0)
  • 2020-12-03 06:42

    I also ran into the problem to update more than 500 documents inside a Firestore collection. And i would like to share how i solved this problem.

    I use cloud functions to update my collection inside Firestore but this should also work on client side code.

    The solution counts every operation which is made to the batch and after the limit is reached a new batch is created and pushed to the batchArray.

    After all updates are completed the code loops through the batchArray and commits every batch which is inside the array.

    It is important to count every operation set(), update(), delete() which is made to the batch because they all count to the 500 operation limit.

    const documentSnapshotArray = await firestore.collection('my-collection').get();
    
    const batchArray = [];
    batchArray.push(firestore.batch());
    let operationCounter = 0;
    let batchIndex = 0;
    
    documentSnapshotArray.forEach(documentSnapshot => {
        const documentData = documentSnapshot.data();
    
        // update document data here...
    
        batchArray[batchIndex].update(documentSnapshot.ref, documentData);
        operationCounter++;
    
        if (operationCounter === 499) {
          batchArray.push(firestore.batch());
          batchIndex++;
          operationCounter = 0;
        }
    });
    
    batchArray.forEach(async batch => await batch.commit());
    
    return;
    
    0 讨论(0)
  • 2020-12-03 06:42

    Your solution will not really be "recursive". If you want to batch update a collection with more than 500 documents, you will have to iterate the documents, create batches of 500 or less on your own, then commit the batches individually. Or, you can simply update each document individually, as there might not be any real need for batches in your situation.

    0 讨论(0)
提交回复
热议问题