如何在Firestore中按日期正确执行查询? [英] how to properly perform query by date in Firestore?

查看:53
本文介绍了如何在Firestore中按日期正确执行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行cronjob来删除使用http触发器传递的事件.今天是9月7日,因此如下图所示的9月5日(6fqmROcWD7K1pTFtXmJJ)事件将在查询快照中捕获.但文档长度为零

I am trying to make a cronjob to delete an event that has been passed by using http trigger. today is 7th September, so the event at 5 september ( 6fqmROcWD7K1pTFtXmJJ )like the picture below shall be captured in the query snapshot. but the document length is zero

这是我使用的代码:

export const cronJobDeletingPastEvents = functions.https.onRequest(async (request,response) => {

        const dateNow = Date.now()
        const pastEventsSnapshot = await admin.firestore().collection("events").where('dateTimeFinish', '<', dateNow).get()
        console.log(pastEventsSnapshot.docs.length) // <---- the result is zero

// perform some actions ....

}

我不知道这里出了什么问题,但是我想这是因为dateNow是数字,但是firestore中的字段dateTimeFinishTimestamp对象.但我不知道该如何解决

I don't know what went wrong in here, but I guess this is because dateNow is number, but the field dateTimeFinish in firestore is Timestamp object. but i dont know how to fix that

推荐答案

以下方法将起作用:

const dateNow = new Date()

const pastEventsSnapshot = await admin.firestore().collection("events").where('dateTimeFinish', '<', dateNow).get()
...

以下内容也可以工作:

const dateNow = new Date('2018-09-07T01:25:54.000Z')
const dateNow = new Date('2018-09-07')

这篇关于如何在Firestore中按日期正确执行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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