【问题标题】:Batch updates reporting contention error using nodejs使用 nodejs 批量更新报告争用错误
【发布时间】:2020-09-22 03:13:33
【问题描述】:

我正在尝试在集合中更新,那里有 1400 多个办公室,在检查并运行查询后,我在集合文档中更新并在查询后在子集合中更新,但有时我会收到此错误 10 中止:对这些文件的争论过多。请再试一次。 我只是使用批处理在文档中编写这里是我在集合中更新的代码。

batch.set(
  rootCollections.x.doc(getISO8601Date())
    .collection(subCollection.y)
    .doc(change.after.get('Id')),
  {
    officeId: change.after.get('Id'),
    office: change.after.get('office'),
    status: change.after.get('status'),
    creationTimestamp:
      change.after.get('creationTimestamp') ||
      change.after.get('createTimestamp') ||
      change.after.get('timestamp'),
    activeUsers: [...new Set(phoneNumbers)].length,
    confirmedUers: activityCheckinSubscription.docs.length,
    uniqueActivities: [...new Set(activities)].length,
    payments: 0,
    contact: [
      `${change.after.data().creator.phoneNumber},${
        change.after.data().attachment['First Contact'].value
      }`,
    ],
  },
  { merge: true },
);
batch.set(
  rootCollections.x.doc(getISO8601Date()),
  {
    Added: admin.firestore.FieldValue.increment(1),
  },
  { merge: true },
);
PromiseArray.push(batch.commit());
await Promise.all(PromiseArray); 

【问题讨论】:

    标签: node.js google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您似乎在这个类似的案例here 中面临同样的问题,其中数据库中有数千条记录正在更新。正如那里澄清的那样,您在一秒钟内可以在文档中执行多少写入是有限的 - 更多细节here - 即使 Firestore 有时可能会挂起更快的写入,它会在某个时候失败。

    由于这是硬编码并且它是由 Firestore 施加的限制,您可以尝试的是在类似案例here 中解释的解决方案,它要么更改为实时数据库,其中限制不是写入次数,但数据的大小或在 Firestore 中使用计数器或其他一些数据聚合的情况下,要使用分布式计数器解决方案,您可以获得更多详细信息here

    总而言之,除非使用此解决方案解决此问题,否则您无能为力,因为这是 Firestore 记录的限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-28
      • 2014-12-13
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多