Firestore query with timestamp

Viewed 7031

I am able to fetch the data using where condition if it's a text field but when I am trying to do the same with timestamp field and date things are not working.

Here is my code:

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(", "));
    });

Everything works fine with firebase.firestore().collection("cities").where("state", "==", "CA") but not working with date.

Screenshot of fire storage:

enter image description here

Note: JLD doc should be returned in console because its timestamp value is greater than 3/24/2020

Edit 1

As guided by @alex now I am doing like this

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

but it is including the data of 2020-03-25 when I am using >, why ?

1 Answers

I am trying to do the same with the timestamp field and date things are not working.

It's not working because you are passing to the .where("timestamp", ">", "3/24/2020") a String which is not correct since your timestamp property in the database holds a Date object and not a String. To solve this, instead of passing the date as a String ("3/24/2020") pass it as a Date object and everything will work fine.

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

And then use:

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