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.
- I want to listen for any newer data that has a
createdDummyTimehigher then with what I'm querying with. - 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?