最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What counts towards the 500-item limit in firestore batch writes? - Stack Overflow

programmeradmin0浏览0评论

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)?

Share Improve this question edited Feb 13, 2019 at 15:18 Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges asked Feb 13, 2019 at 13:37 dsl101dsl101 1,78418 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

With 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.

发布评论

评论列表(0)

  1. 暂无评论