Kid
Kid

Reputation: 2205

Can a firestore snapshot contain x number of changed documents

I want to ask about Firestore

Reading the docs

When a document is added, removed or modified then I get a signal about that event here in this code from the doc:

db.collection("cities").where("state", "==", "CA")
    .onSnapshot((snapshot) => {
        snapshot.docChanges().forEach((change) => {
            if (change.type === "added") {
                console.log("New city: ", change.doc.data());
            }
            if (change.type === "modified") {
                console.log("Modified city: ", change.doc.data());
            }
            if (change.type === "removed") {
                console.log("Removed city: ", change.doc.data());
            }
        });
    });
  1. When I start this listener will I then get all document matching "state", "==", "CA" even if it's 100.000 of them? Will they come all at once or in batches?

  2. after the above initial all (100.000) will I after that always get one(1) document, like when a doc is modified, added or removed, or can there be like a batch collapsing latency from firestore so I will get 1-to-many in the snapshot?

Upvotes: 1

Views: 338

Answers (2)

Dharmaraj
Dharmaraj

Reputation: 50830

You get only the documents that were added/modified/removed however the documentation clearly states the first time when the listener is set up it will return all the matching documents documents.

The first query snapshot contains added events for all existing documents that match the query. This is because you're getting a set of changes that bring your query snapshot current with the initial state of the query. This allows you, for instance, to directly populate your UI from the changes you receive in the first query snapshot, without needing to add special logic for handling the initial state.

So they do come all along. After that they'll be only the changes i.e. the amount of documents being changed.

The initial state can come from the server directly, or from a local cache. If there is state available in a local cache, the query snapshot will be initially populated with the cached data, then updated with the server's data when the client has caught up with the server's state.

Now with that being said you may be charged for it if the data is coming from the server and not from the local cache.

Upvotes: 1

Jason Berryman
Jason Berryman

Reputation: 4898

When you first run the query, you'll get everything that matches that query, with a change.type === "added". Then you will receive changes as they are made, one by one (unless someone writes a batch at once).

The way to manage this is to add a filter to the collection. For example, you may want to sort the collection by a date field or a name field. Then limit the results to a manageable number and paginate.

db.collection("cities")
    .where("state", ">=", "CA")
    .orderBy("state")
    .limit(50)
    .onSnapshot((snapshot) => {
        snapshot.docChanges().forEach((change) => {
            if (change.type === "added") {
                console.log("New city: ", change.doc.data());
            }
            if (change.type === "modified") {
                console.log("Modified city: ", change.doc.data());
            }
            if (change.type === "removed") {
                console.log("Removed city: ", change.doc.data());
            }
        });
    });

Don't forget to add an unsubscribe so that you can remove the listener

Upvotes: 2

Related Questions