如何在Firebase Cloud Function中查询对象数组,以获取匹配的对象然后更新

问题描述

我正在Firebase Cloud Function中使用调度的任务来查询一个数组,该数组包含多个存在匹配条件时需要更新的对象。我目前的尝试是使用“数组包含”方法获取对象,然后遍历它们以找到匹配的条件,然后将其批量更新。这是我的数据结构:

enter image description here

我需要找到一个

export const liveMeetingsTrigger = functions.runWith( { memory: '1GB' }).pubsub

    .schedule('every 1 minutes').onRun(async context => {

        const Now = admin.firestore.Timestamp.Now();

        const liveMeetings = await admin.firestore().collection('fl_content').where('meeting','array-contains','liveMeetingDate').get();

        const batch = admin.firestore().batch();
        
        liveMeetings.forEach(doc => {

          if(doc.data().liveMeetingDate <= Now && doc.data().active == false){
             batch.update(doc.ref,'active',true);
          }

        });
        
        return await batch.commit();

});

我也尝试过在查询中使用确切的对象,而不仅仅是使用'liveMeetingDate',但是仍然没有返回结果,任何帮助都将非常有用-谢谢。

调试:由于我要到达的数组位于(map)对象'liveMeetings'的内部,因此我尝试了点表示法(liveMeetings.meeting),但未成功。另外,尝试在顶层使用“会议”数组的新集合也没有成功。

控制台中的简单日志记录(liveMeetings.size)显示查询没有返回任何内容,因此日志记录甚至未到达代码中的循环。

enter image description here

解决方法

如本anwser中所述,以下查询将不起作用:

const liveMeetings = await admin.firestore().collection('fl_content').where('meeting','array-contains','liveMeetingDate').get();

因为meetings数组包含一些对象,而不是“简单”或原始数据(例如字符串,数字...)。

您可以使用确切的对象进行查询,例如:

const obj = {active: false,liveMeetingDate: ...,meetingId: ...,....};
    const liveMeetings = await admin.firestore().collection('fl_content').where('meeting','obj').get();

另一种方法是创建一个新集合,该集合包含相似的文档(相同的文档ID),但带有一个meeting数组,该数组仅包含liveMeetingDate属性。


最后,请注意,由于数组位于地图内,因此您需要这样做

await admin.firestore().collection('fl_content').where('liveMeetings.meeting',...).get();

(PS:由于您明确要求在重复的问题/答案的注释中寻求更多帮助,因此我不会将此问题标记为重复)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...