本文介绍了有没有办法在云端功能中更新collectionGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个聊天应用程序.当用户在其本地配置文件上进行更新时,我想使用云功能在整个collectionGroup上进行更新.

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.

我正在成功收听云功能的更新,并使用以下内容检索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();

        });

但是现在我需要更新该collectionGroup中的一个字段,这就是我遇到问题的地方.

But now I need to update a field within that collectionGroup and that's where I'm running into issues.

collectionGroup存储在2个位置:

The collectionGroup is stored in 2 locations:

users{id}collectionGroupName{id}
groups{id}collectionGroupName{id}

是否可以更新该collectionGroup中的所有文档

Is it possible to update all of the documents in that collectionGroup

推荐答案

Firestore不提供任何方法来更新整个集合或集合组,例如SQL中的"UPDATE WHERE".相反,您需要做的是分别编写每个文档.因此,如果您已经对集合组中的文档执行了查询,是否可以简单地对结果集中的文档进行迭代并根据需要更新每个文档.您可以使用 DocumentSnapshot 的ref属性轻松地更新每个文档集合包含它.

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(...)
})

这篇关于有没有办法在云端功能中更新collectionGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 01:04