【发布时间】:2025-12-02 15:30:01
【问题描述】:
我有以下云函数,想知道我应该使用批量写入还是事务:
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()
我确实要求此操作永远不会失败,但是,我没有看到任何会失败的情况。
在这种情况下是否有任何理由使用事务?
相反,这里有什么理由不使用事务吗?
【问题讨论】:
标签: firebase google-cloud-firestore google-cloud-functions