如何使用批处理在Firestore中更新500多个文档? [英] How can I update more than 500 docs in Firestore using Batch?

查看:66
本文介绍了如何使用批处理在Firestore中更新500多个文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用具有超过500个文档的集合中的Firestore管理员时间戳来更新字段timestamp.

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

const batch = db.batch();
const serverTimestamp = admin.firestore.FieldValue.serverTimestamp();

db
  .collection('My Collection')
  .get()
  .then((docs) => {
    serverTimestamp,
  }, {
    merge: true,
  })
  .then(() => res.send('All docs updated'))
  .catch(console.error);

这会引发错误

{ Error: 3 INVALID_ARGUMENT: cannot write more than 500 entities in a single call
    at Object.exports.createStatusError (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\common.js:87:15)
    at Object.onReceiveStatus (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:1188:28)
    at InterceptingListener._callNext (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:564:42)
    at InterceptingListener.onReceiveStatus (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:614:8)
    at callback (C:\Users\Growthfile\Desktop\cf-test\functions\node_modules\grpc\src\client_interceptors.js:841:24)
  code: 3,
  metadata: Metadata { _internal_repr: {} },
  details: 'cannot write more than 500 entities in a single call' }

有没有一种方法可以编写一个递归方法,该方法创建一个批处理对象,逐个更新一批500个文档,直到所有文档都被更新.

Is there a way that I can write a recursive method which creates a batch object updating a batch of 500 docs one by one until all the docs are updated.

从文档中,我知道可以使用此处提到的递归方法进行删除操作:

From the docs I know that delete operation is possible with the recursive approach as mentioned here:

https://firebase.google.com/docs/firestore/manage-data/delete-data#collections

但是,对于更新,由于文档并未被删除,我不确定如何结束执行.

But, for updating, I'm not sure how to end the execution since the docs are not being deleted.

推荐答案

我还遇到了更新Firestore集合中500多个文档的问题.我想分享我如何解决这个问题.

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.

我使用云功能在Firestore中更新我的收藏集,但这也应该适用于客户端代码.

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

该解决方案计算对批次进行的每个操作,并在达到限制后创建新批次并将其推送到batchArray.

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.

所有更新完成后,代码将循环遍历batchArray并提交数组内部的每个批处理.

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

重要的是要对批次进行的每个操作进行计数 set(), update(), delete(),因为它们全部都计入500个操作限制.

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;

这篇关于如何使用批处理在Firestore中更新500多个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆