如何在Firebase Cloud Function中查询对象数组,以获取匹配的对象然后更新 [英] How to query an array of objects in a Firebase Cloud Function, to get a matching object and then update

查看:89
本文介绍了如何在Firebase Cloud Function中查询对象数组,以获取匹配的对象然后更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Firebase Cloud Function中使用调度的任务来查询一个数组,该数组包含多个存在匹配条件的对象,这些对象需要更新.我当前的尝试是使用'array-contains'方法获取对象,然后遍历它们以找到匹配的条件,然后对这些条件进行批处理更新.这是我的数据结构:

I am using a scheduled task in a Firebase Cloud Function to query an array which contains a number of objects that need to be updated if a matching condition exists. My current attempt is using the 'array-contains' method to get the objects, then loop over them to find a matching condition which will then batch update the items. This is my data structure:

我需要找到一个< =当前时间的对象,并且如果'active'值= false.

I need to find an object that is <= the current time, and also if the 'active' value = false.

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',但是仍然没有返回结果,任何帮助都将非常有用-谢谢.

I have also tried using an exact object in the query instead of just using 'liveMeetingDate', but still get no results back, any help would be great - thanks.

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

Debugging: As the array I am trying to reach is inside of the (map) object 'liveMeetings' i have tried the dot notation (liveMeetings.meeting) with no success. Also trying a new collection with the the 'meeting' array at top level has provided no success.

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

Simple logging in the console (liveMeetings.size) shows that nothing is being returned on the query, so therefore the logging does not even reach the loop in the code.

推荐答案

如此答案中所述,以下查询将不起作用:

As explained in this anwser the following query will not work:

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

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

because the meetings array contain some objects, instead of "simple" or primitive data (e.g. string, number...).

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

You could query it with the exact objects, like:

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


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


Another approach would be to create a new collection which contains the similar documents (same Document ID) but with a meeting Array that contains only the liveMeetingDate property.

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

Finally, note that since your Array is within a map, you need to do

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


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


(PS: I don't mark this question as duplicate since you expressly ask for more help in the comments of the duplicate question/answer)

这篇关于如何在Firebase Cloud Function中查询对象数组,以获取匹配的对象然后更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆