admin管理员组文章数量:1353129
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 listening to the update in cloud functions and retrieving a list of collectionGroups with the following:
const collectionGroupNameref = await db.collectionGroup('collectionGroupName').where('userId', '==', data.uid).get();
collectionGroupNameref.forEach(async (val: any) => {
const connectionsRef = await db.collection('collectionGroupName').doc(val.id).get();
});
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 listening to the update in cloud functions and retrieving a list of collectionGroups with the following:
const collectionGroupNameref = await db.collectionGroup('collectionGroupName').where('userId', '==', data.uid).get();
collectionGroupNameref.forEach(async (val: any) => {
const connectionsRef = await db.collection('collectionGroupName').doc(val.id).get();
});
But now I need to update a field within that collectionGroup and that's where I'm running into issues.
The collectionGroup is stored in 2 locations:
users{id}collectionGroupName{id}
groups{id}collectionGroupName{id}
Is it possible to update all of the documents in that collectionGroup
Share Improve this question edited Nov 26, 2019 at 3:17 Doug Stevenson 318k36 gold badges456 silver badges473 bronze badges asked Nov 26, 2019 at 1:28 D.HodgesD.Hodges 2,0994 gold badges27 silver badges60 bronze badges2 Answers
Reset to default 13Firestore 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(...)
})
There is another approach you can use :
1- Get All objects in a collection group that you want to update (v1)
export const getAllItemsFromCollectionGroup = async <T>(collection: string): Promise<T[]> => {
const items: T[] = [];
const db = admin.firestore();
const query = await db.collectionGroup(collection).get();
if (query && !query.empty) {
query.docs.forEach(doc => {
items.push({
...doc.data() as T,
id: doc.id,
path: doc.ref.path
});
});
}
return items;
};
Note that when I can loop through returned objects, I add the absolute path to each object.
2- Now you filter your objects based on path ou subpath your are interested in.
3- Bulk update using their path
本文标签: javascriptIs there a way to update a collectionGroup in cloud functionStack Overflow
版权声明:本文标题:javascript - Is there a way to update a collectionGroup in cloud function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743906110a2559554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论