I want to ask about Firestore
Reading the docs
When a document is added, removed or modified then I get a signal about that event here in this code from the doc:
db.collection("cities").where("state", "==", "CA")
.onSnapshot((snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === "added") {
console.log("New city: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified city: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Removed city: ", change.doc.data());
}
});
});
When I start this listener will I then get all document matching "state", "==", "CA"
even if it's 100.000 of them? Will they come all at once or in batches?
after the above initial all (100.000) will I after that always get one(1) document, like when a doc is modified, added or removed, or can there be like a batch collapsing latency from firestore so I will get 1-to-many in the snapshot?
When you first run the query, you'll get everything that matches that query, with a change.type === "added"
. Then you will receive changes as they are made, one by one (unless someone writes a batch at once).
The way to manage this is to add a filter to the collection. For example, you may want to sort the collection by a date field or a name field. Then limit the results to a manageable number and paginate.
db.collection("cities")
.where("state", ">=", "CA")
.orderBy("state")
.limit(50)
.onSnapshot((snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === "added") {
console.log("New city: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified city: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Removed city: ", change.doc.data());
}
});
});
Don't forget to add an unsubscribe so that you can remove the listener