firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

(Firestore) Allow onSnapshot query to return 0 initial results; only update on future changes

Open shaunc869 opened this issue 9 years ago • 4 comments

[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!

shaunc869 avatar Oct 26 '17 03:10 shaunc869

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(...

mikelehen avatar Nov 28 '17 00:11 mikelehen

Thank you, really appreciate this can be done!

shaunc869 avatar Nov 28 '17 01:11 shaunc869

@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 avatar Aug 09 '18 18:08 aaronleo28

@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.

mikelehen avatar Aug 09 '18 21:08 mikelehen