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);
This throws an 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' }
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…