【发布时间】:2019-02-09 10:21:43
【问题描述】:
我正在尝试使用包含 500 多个文档的集合中的 Firestore 管理员时间戳更新字段 timestamp。
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 个文档,直到所有文档都被更新。
从文档中我知道使用此处提到的递归方法可以进行删除操作:
https://firebase.google.com/docs/firestore/manage-data/delete-data#collections
但是,为了更新,我不确定如何结束执行,因为文档没有被删除。
【问题讨论】:
-
为什么不遍历所有 500 个文档,更新并使用最后一个文档键来构造 startAt 以创建新查询?
-
您可以限制然后递归批处理,遇到同样的问题,这是我的解决方案:stackoverflow.com/a/61639536/2195000
标签: javascript firebase google-cloud-firestore firebase-admin