FireStore batched writes with over 500 operations error

2019-10-03

firestore support a batches writes. Official document say that it should be used when multiple data are written, modified or deleted.

What’s cases FireStore batched writes?

For example, a flug in all documents that have been gotten from `hoge` collection must be modified as the following.

	const batch = db.batch();
	db.collection('hoge').get()
        .then(snapshot => {
            snapshot.forEach(doc => {
               batch.update(doc.ref, {flg: true});
            });
        });
    return batch.commit();

batch.commit() return Promise, so use await.

Up to 500 operations

If batches writes over 500 operations, it occur errors with FireStore.

Error: 3 INVALID_ARGUMENT: maximum 500 writes allowed per request


“A batched write can contain up to 500 operations." in official document.

The following may be good way that over 500 document are writen in bulk.

	let batch = db.batch();
	db.collection('hoge').get()
        .then(snapshot => {
        	let cnt = 0;
            snapshot.forEach(doc => {
               cnt++
               if (cnt > 500) {
                   batch.commit();
                   batch = db.batch();
               }
               batch.update(doc.ref, {flg: true});
            });
        });
    return batch.commit();

It should be set transaction because of ensuring all “batch.commit". (omit in above)
It seems that the way concomitant use of batch with transaction is not described in document.

It is not usually required quickly processing in the case of batched writing huge data over 500. So I think that it is better to write slowly each by one data with transactions.

On the other hand, it should be use batch operation when data saving is certainly within 500 and all operations are same.