Firestore receive new data with querying time based data

Viewed 30

I wanted to structure data in a way where I can get updates on newer data and ignore old data. So I have added a time field and thought to add a where clause and a onSnapshot listener.

Code for collection creation:

const citiesRef = db.collection('cities');

await citiesRef.doc('SF').set({
  createdDummyTime: new Date().toISOString(), //or just Date()
  name: 'San Francisco', population: 860000,
});

Code for collection update:

citiesRef 
  .doc('SF')
  .update({ 
    createdDummyTime: new Date().toISOString(),
    name: 'San Francisco', population: 999999999,
    }),
  })
  .then(() => {
    console.log("res");
  });

//or set another doc('blahblah') with time higher then querying with.

Code for listening data:

db.collection("cities").where("createdDummyTime", ">", new Date().toISOString())
.onSnapshot((querySnapshot) => {
    var cities = [];
    querySnapshot.forEach((doc) => {
        cities.push(doc.data().name);
    });
    console.log("Current cities in CA: ", cities.join(", "));
});

I received nothing, onSnapshot, on my test, maybe I'm doing wrong.

  1. I want to listen for any newer data that has a createdDummyTime higher then with what I'm querying with.
  2. This may not be possible since onSnapshot on query does not know the new data that will be added, but this is syntactically correct. Also if I update existing data with new time and other value. If not possible then what other way I can achieve this?
0 Answers
Related