Firestoreに一括で更新を行う
やりたいこと
Firestoreにて、複数のドキュメントに対して一括で更新を行いたい。
バッチ書き込みを利用
.batch()で生成したオブジェクトにupdateしたいパスとオブジェクトをガンガン足していく。batch.commit()
したタイミングで一括実行される。
import firebase from 'firebase/app' import firestore from '@/firebase/firebase' ... const batch = firestore.batch() cardList.forEach(card => { const cardRef = firestore.collection('card').doc(card.id) batch.update(cardRef, { index: card.newIndex }) }) batch.commit()