【问题标题】:how to properly perform query by date in Firestore?如何在 Firestore 中按日期正确执行查询?
【发布时间】:2019-02-12 18:22:21
【问题描述】:

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

这是我使用的代码:

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 对象。但我不知道如何解决这个问题

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    以下将起作用:

    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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2018-04-10
      • 2014-01-29
      • 1970-01-01
      • 2021-01-15
      相关资源
      最近更新 更多