Does collection$.valueChanges() return only changes or the whole collection?

Viewed 778

I'm a bit unclear what valueChanges actually does -- if my subscription looks like this:

(Typescript/Angular)

    this.accountsCollection = this.firestore.collection(`budgets/${budgetId}/accounts`);
    this.accounts$ = this.accountsCollection.valueChanges();
    this.accountsSubscription = this.accounts$.subscribe(accountData => {
      AccountsManager.getInstance().loadData(accountData)
    })

Am I going to get the whole collection each time a collection document is updated, added, deleted etc, or just the document that changed.

If I only wanted to see changes in the subscription (added/updated/deleted) how can get a subscription which does that?

1 Answers

Your callback will receive the entire set of documents any time any document is added, changed or removed. The set of documents is cached in memory for as long as the listener is active, and it will not require each document to be transferred every time - only the deltas are actually sent over the connection.

I suggest reading through the documentation for streaming collection data.

In your code, if you want to know which documents are actually changed, valueChanges will not help you discern that. As the documentation states:

Why would you use it? - When you just need a list of data. No document metadata is attached to the resulting array which makes it simple to render to a view.

You should instead use snapshotChanges(). It will give you DocumentChangeAction type objects that you can use to tell you which documents actually changed.

Related