I have the following code in a cloud function which is returning an error with the message
Error: 3 INVALID_ARGUMENT: maximum 500 writes allowed per request
console.log(`${projectId} doClassifySources: Got ${_.size(output)} items`)
const lastClassification = new Date().toJSON()
const batch = firestore.batch()
batch.update(projectRef, {lastClassification})
_.forEach(output, item => {
batch.set(projectRef.collection('output').doc(encodeURIComponent(item.url)), {
classifiedAt: admin.firestore.FieldValue.serverTimestamp(),
...item
}, {
merge: true
})
})
return batchmit().then(() => lastClassification)
Yet the firebase logs show this right before the error is thrown:
12:28:19.963 PM classifySources ZklZYB5hq96J43CroKgP doClassifySources: Got 310 items
That looks to me like the batch should contain 310 items, well below the 500 limit. Am I missing something in how that 500-item limit is calculated? Does the merge: true
influence that in any way? Is it to do with the spread of item
in the object being written (i.e. does that increase the number of writes needed)?
I have the following code in a cloud function which is returning an error with the message
Error: 3 INVALID_ARGUMENT: maximum 500 writes allowed per request
console.log(`${projectId} doClassifySources: Got ${_.size(output)} items`)
const lastClassification = new Date().toJSON()
const batch = firestore.batch()
batch.update(projectRef, {lastClassification})
_.forEach(output, item => {
batch.set(projectRef.collection('output').doc(encodeURIComponent(item.url)), {
classifiedAt: admin.firestore.FieldValue.serverTimestamp(),
...item
}, {
merge: true
})
})
return batch.commit().then(() => lastClassification)
Yet the firebase logs show this right before the error is thrown:
12:28:19.963 PM classifySources ZklZYB5hq96J43CroKgP doClassifySources: Got 310 items
That looks to me like the batch should contain 310 items, well below the 500 limit. Am I missing something in how that 500-item limit is calculated? Does the merge: true
influence that in any way? Is it to do with the spread of item
in the object being written (i.e. does that increase the number of writes needed)?
1 Answer
Reset to default 21With some additional testing, it appears that using admin.firestore.FieldValue.serverTimestamp()
counts as an extra 'write'. With the code as above, I'm limited to 249 items (i.e. when output.length >= 250
, the code will fail with that error message. If I remove the reference to serverTimeStamp()
, I can get up to 499 items per write.
I can't find this documented anywhere, and perhaps it is a bug in the firebase library, so I will post an issue there and see what happens.