【问题标题】:Can I change in Firestore all documents in who correspond to a certain condition without querying for all of them first?我可以在不先查询所有文档的情况下在 Firestore 中更改与某个条件相对应的所有文档吗?
【发布时间】:2019-07-25 05:51:30
【问题描述】:

我正在建立某种社交网络。为了保存读取操作,对于每篇文章,我都会将作者的详细信息(图像和名称)存储在其中。 我正在尝试创建一个云功能,当用户编辑他们的个人资料详细信息时,该功能将更改用户撰写的所有帖子并相应地更新它们(使用新名称/图像)。

我面临的问题(不是什么大问题,只是想提高效率),我如何编辑该用户的所有帖子? 我是否必须先查询该用户的所有帖子,然后遍历结果以更改它们? 或者有什么方法可以让我编辑所有符合特定条件的文档?

这是我现在的函数,我放在了我希望存在的假设函数中。

exports.updateUser = functions.firestore.document('communities_data/{community}/profiles/{profileID}').onUpdate((change, context) => {
    // Retrieve the current and previous value
    const data = change.after.data();
    const previousData = change.before.data();

    // We'll only update if the name has changed.
    // This is crucial to prevent infinite loops.
    if (data !== undefined && previousData !== undefined) {

        if (data.name !== previousData.name || data.image !== previousData.image) {

           //I am looking for something like this
            return db.doc('communities_data/' + context.params.community + '/questions').where('author_ID', '==', context.params.profileID).set

        } else {
            return null;
        }
    } else {
        return null;
    }
});

【问题讨论】:

    标签: typescript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    使用 Cloud Firestore,没有像 SQL “update where”这样的批量更新机制。您必须查询要更新的文档,迭代结果并分别更新它们。

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 2019-06-21
      • 2012-02-24
      • 1970-01-01
      • 2020-11-08
      • 2018-05-09
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多