Is There A Way To Update A Collectiongroup In Cloud Function
I'm building a chat app. When a user makes an update on their local profile I'd like to use cloud functions to make that update across a collectionGroup. I'm successfully listenin
Solution 1:
Firestore doesn't provide any methods to update an entire collection or collection group like "UPDATE WHERE" in SQL. What you are required to do instead is write each document individually. So, if you've already executed a query for documents in the collection group, can you simply iterate the documents in the result set and update each document as needed. You can use the ref property of DocumentSnapshot to easily update each document, no matter what collection contains it.
const querySnapshot = await db
.collectionGroup('collectionGroupName')
.where('userId', '==', 'data.uid')
.get();
querySnapshot.docs.forEach(snapshot => {
snapshot.ref.update(...)
})
Post a Comment for "Is There A Way To Update A Collectiongroup In Cloud Function"