0
votes

I was checking out the Flutter firestore dependency and wanted to know how can I get only the documents that are newly added or modified?

I am not sure whether the code will return all snapshots in collection 'abc' first and then filter modified ones?

Because if it returns all snapshots and my collection has 1 million snapshots and only 1 modified then my code will end up reading all 1 million snapshots to get that 1 modified one.

Firestore.instance.collection('abc').snapshots().listen((data){
    data.forEach((change) {
       if(change.type == DocumentChangeType.Modified) {
       }
    })
})
1

1 Answers

2
votes

Listeners on a query (a collection with no filters is still a query) will always receive the entire set of results as "added" documents in the first callback. Later changes to those documents will show up as modifications to those documents.

So, if your collection has 1 million documents in it, this query will attempt to read the entire set, which is probably not what you want. You will need a way to filter the collection using a where clause. Consider storing a timestamp in each document, and querying for only the documents after that timestamp if you just want incremental updates over time.