繁体   English   中英

带时间戳的 Firestore 查询

[英]Firestore query with timestamp

如果它是一个文本字段,我可以使用 where 条件获取数据,但是当我尝试对时间戳字段和日期执行相同操作时,事情不起作用。

这是我的代码:

home.ts

firebase.firestore().collection("cities").where("timestamp", ">", "3/24/2020")
    .onSnapshot(function(querySnapshot) {
        var cities = [];
        querySnapshot.forEach(function(doc) {
            cities.push(doc.data().name);
        });
        console.log("Timestamp greather than 3/24/2020 -- ", cities.join(", "));
    });

firebase.firestore().collection("cities").where("state", "==", "CA")一切正常firebase.firestore().collection("cities").where("state", "==", "CA")但不适用于date

消防栓截图:

在此处输入图片说明

注意: JLD文档应该在控制台中返回,因为它的timestamp值大于3/24/2020

编辑 1

在@alex 的指导下,我现在正在这样做

let start = new Date('2020-03-25'); firebase.firestore().collection("cities").where("timestamp", ">", start)

但是当我使用>时它包括2020-03-25的数据,为什么?

我正在尝试对时间戳字段执行相同的操作,但日期无法正常工作。

它不起作用,因为您正在向.where("timestamp", ">", "3/24/2020")一个不正确的字符串,因为您在数据库中的timestamp属性包含一个Date对象而不是一个字符串。 为了解决这个问题,不要将日期作为字符串(“3/24/2020”)作为日期对象传递,一切都会正常工作。

let timestamp = new Date('2020-03-25');

然后使用:

firebase.firestore().collection("cities").where("timestamp", ">", timestamp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM