Tracking of changes in collection

In documentation we see.

stateChanges()

What is it? - Returns an Observable of the most recent changes as a DocumentChangeAction[].

In code we see https://github.com/angular/angularfire2/blob/fdf6c9bfeae37e9c781f0d24b590e23f7b658c0e/src/firestore/collection/collection.ts#L59

Listen to the latest change in the stream. This method returns changes as they occur and they are not sorted by query order. This allows you to construct your own data structure.

in reality

this._store
        .collection(entity.path)
        .stateChanges().pipe(
           tap(x=>console.log(x))
        ).subscribe();

Console says:

0: {type: "added", payload: {…}}
1: {type: "added", payload: {…}}
2: {type: "added", payload: {…}}
3: {type: "added", payload: {…}}
4: {type: "added", payload: {…}}
...

we see that all changes from all history were logged.

Is there way to observe only changes from moment I started to observe?

Is it bug or intended behaviour?

1 answer

  • answered 2019-10-15 16:34 Doug Stevenson

    This is working as expected. Your code is querying for every document in the collection identified by entity.path, so you can expect to receive every document in the initial set of results. If you don't want all the documents in the collection, you will need to come up with a "where" clause filter that identifies which documents you do want. If your interest is based on the time of the query, consider adding a timestamp field to each document so you can use it in a filter that will give you only documents that will appear after the current moment in time.