I have the following Cloud Function and want to find out whether I should use batched writes or a transaction:
const firestore = admin.firestore()
// The following two queries potentially return hundreds of documents.
const queryA = firestore.collectionGroup('a').where('b', '==', 'c'),
queryB = firestore.collection('b').where('b', '==', 'c')
const snapshotA = await queryA.get(), snapshotB = await queryB.get()
const batch = firestore.batch()
for (const documentSnapshot of snapshotA.docs.concat(snapshotB.docs)) {
batch.update(documentSnapshot.ref, { 'b': 'd' })
}
return batch.commit()
I do require this operation to never fail, however, I do not see any case this would ever fail.
Is there any reason to use a transaction instead in this case?
Conversely, is there any reason not to use a transaction here?