Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firestore - listen to update on the entire collection

I'm using google's firestore and I want to get a live update on the entire collection.

I saw this in the documents: https://cloud.google.com/nodejs/docs/reference/firestore/0.11.x/CollectionReference#onSnapshot

but as far as I understood I have to get a document or to query a range of documents.

how can I listen to changes in the entire collection?

like image 962
Elon Salfati Avatar asked Feb 04 '18 09:02

Elon Salfati


2 Answers

If you refer back to the Listen to multiple documents in a collection documentation, simply omit the where filter and you should get the whole collection.

The first line would look like this: var query = db.collection("cities");

like image 79
Jason Berryman Avatar answered Oct 24 '22 21:10

Jason Berryman


From my current experience, .onSnapshot without query (.where) helps you listen to the whole collection. E.g

   db.collection('cities')..onSnapshot(function(querySnapshot) {
      var cities = [];
      querySnapshot.forEach(function(doc) {
          cities.push(doc.data().name);
      });
      console.log("Current cities in CA: ", cities.join(", "));
    });

Source: https://firebase.google.com/docs/firestore/query-data/listen

You can read more there too.

like image 41
Bembem Avatar answered Oct 24 '22 21:10

Bembem