(Firestore) Allow onSnapshot query to return 0 initial results; only update on future changes
[REQUIRED] Describe your environment
Operating System version: Mac OS High Sierra Firebase SDK version: 4.5.2 Firebase Product: firestore database
[REQUIRED] Describe the problem
Firestore's ability to automatically report updates on a query set is nothing short of amazing, but when you first run an onSnapshot with a query such as the following:
db.collection("contacts").where("company_id", "==", company_id)
.onSnapshot(function (querySnapshot) {
var contacts = [];
querySnapshot.forEach(function (doc) {
contacts.push(doc.data())
});
cb(contacts)
});
initially it grabs all of the contacts, but this could be hundreds or thousands, when in reality you don't need all of these now or perhaps ever in the app, but you do want to know about future changes to this dataset.
It would be great if you could tell Firestore to not return the entire dataset initially and only return future changes to this snapshot/query set. Thanks!
The way to achieve this is to e.g. have a last_updated field on documents and then do a query like
db.collection('contacts').where('company_id', '==', company_id).where('last_updated' > [new Date() or the last "processed" update]).onSnapshot(...
Thank you, really appreciate this can be done!
@mikelehen can this be done on an object propert, eg
db.collection('contacts').where(`company.${companyId}`, '==', true).where('last_updated', '>', 10000)
apologies for commenting on a closed issue
@aaronleo28 There's no particular difference between querying on a top-level field (e.g. 'company') or a nested field (e.g. 'company.foo'), so that should work fine.