Listen only to additions to a cloud firestore collection?

12,312

Solution 1

When you use onSnapshot() on a collection, you're not actually downloading the entire collection on each invocation. The documents are cached and will be reused when the collection changes again.

For each change that causes your callback to be invoked, you can find out which documents are new seen since the first invocation by checking the changes within the snapshot. An example of how to do this is in the documentation. With a query snapshot in hand, you can use this logic to determine which documents are new:

snapshot.docChanges.forEach(function(change) {
    if (change.type === "added") {
        // change.doc here is new a new document
    }
});

Solution 2

It seems that the onSnapshotis only for listening an entire document,which means it would return all of the fields in a document that you're listening. So you are not able to only retrieve the changed field. But you can do different things according to different types of change:

xxxx.onSnapshot(function(querySnapshot){
     querySnapshot.docChanges.forEach(function(change){
                    if(change.type=="added"){//first time it will be triggered

                    }else if(change.type == "modified"){//modified

                    }else if(change.type == "removed"){//removed


                    }
    })
})

Hope this helps

Share:
12,312
billybob2
Author by

billybob2

Updated on June 05, 2022

Comments

  • billybob2
    billybob2 about 2 years

    I've noticed, when I try to use a realtime listener on a Collection in firestore, each time a new Document is added to the collection, the logic will be rerun, and I will download everything already in the collection

    right now:

    firebase.firestore().collection("Tweets").onSnapshot(function(querySnapshot) {
          querySnapshot.forEach(function(doc) {
            console.log("snapshot added ", doc)
          });
        });
    

    Is there a way to track only ADDITIONS to the collection? I guess I could do that device side, but theres no need to transfer all the additional data I have already queryed for..

    Output of that log would print out every single "tweet" in the collection, regardless of only one being added

    EX: Initial Query

    -Tweet 1

    -Tweet 2

    -Tweet 3

    New Tweet, Tweet 4 is added

    Output:

    Tweet 1

    Tweet 2

    Tweet 3

    Tweet 4

    If that makes sense

    • Doug Stevenson
      Doug Stevenson over 6 years
      We need more information here. What is this.DataRef? A collection reference? A query? Please be specific. Also, what is the contents of the database you're working with? What's the log output with those specific contents?
    • billybob2
      billybob2 over 6 years
      sorry, I just made an edit
  • User-8017771
    User-8017771 over 5 years
    Is there any way to listen to a collection only once? something instead of onSnapshot?
  • Doug Stevenson
    Doug Stevenson over 5 years
    Yes, there is get() for that.
  • user1114
    user1114 over 5 years
    Question specified listening for a collection, but this answer specifies listening to a document
  • Teodor Ciuraru
    Teodor Ciuraru about 4 years
    I might be mistaken, but it seems that onSnapshot does download the entire collection on its first run, which doesn't go well with pagination in parallel.
  • BlackCoffee
    BlackCoffee almost 4 years
    "The first query snapshot contains added events for all existing documents that match the query.", how to ignore the first snapshot and only listen for the actual added documents?
  • Doug Stevenson
    Doug Stevenson almost 4 years
    @BlackCoffee You can't. You should instead figure out how to query for only the documents you want. Typically this involves adding a field you can use as a filter. Either that, or you have to come up with your own set of assumptions about what's 'new', based on criteria you define.
  • BlackCoffee
    BlackCoffee almost 4 years
    I figured out a way to ignore the first snapshot, and once a new document was added, the callback function received another full patch of documents (not only the added one) and all of them are of type “Added” again!
  • Doug Stevenson
    Doug Stevenson almost 4 years
    @BlackCoffee If you have a question, please post it separately along with the code that isn't working the way you expect, along with a full explanation.