Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 508
dsl101 Avatar asked Feb 13 '19 13:02

dsl101


People also ask

What is the maximum documents that are write by per transaction or batch of write in cloud firestore?

Each transaction or batch of writes can write to a maximum of 500 documents.

How many documents can firestore hold?

10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.

How many collections can firebase have?

The Firebase Limits does not state any limits on collections, but there is a limit on the depth of sub-collections. The limit is that you can only go 100 subcollections deep, which is very large and you should never reach that point unless you have the most detailed and specific app in the world.

How do you reduce the number of read operations in cloud firestore?

This means that Cloud Firestore will try to retrieve an up-to-date (server-retrieved) snapshot, but fall back to returning cached data if the server can't be reached. This is how we can reduce the number of reads in Cloud Firestore, by forcing the SDK to use only the offline cache and get the data only upon request.


1 Answers

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.

like image 135
dsl101 Avatar answered Oct 19 '22 02:10

dsl101