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

查看:27
本文介绍了如何使用 Batch 在 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:UsersGrowthfileDesktopcf-testfunctions
ode_modulesgrpcsrccommon.js:87:15)
    at Object.onReceiveStatus (C:UsersGrowthfileDesktopcf-testfunctions
ode_modulesgrpcsrcclient_interceptors.js:1188:28)
    at InterceptingListener._callNext (C:UsersGrowthfileDesktopcf-testfunctions
ode_modulesgrpcsrcclient_interceptors.js:564:42)
    at InterceptingListener.onReceiveStatus (C:UsersGrowthfileDesktopcf-testfunctions
ode_modulesgrpcsrcclient_interceptors.js:614:8)
    at callback (C:UsersGrowthfileDesktopcf-testfunctions
ode_modulesgrpcsrcclient_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;

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

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