Home > OS >  Firestore onSnapshot pricing - Listening only for creations
Firestore onSnapshot pricing - Listening only for creations

Time:08-05

In the documentation, we can see this example:

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());
            }
        });
    });

Does it means that the listener is listening for additions, modifications and removals?

For example, if I modify this example to:

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

Will I be charged (pricing) for the documents modifications and removals too?

CodePudding user response:

The document read operations are determined by the query you execute, and the state of the local cache. They are not affected by the code you then use to process the document snapshots.

Your onSnapshot handler gets a snapshot with all documents matching the query. The fact that you then choose to only handle added in the handler has no influence on the pricing.

There is no API to just listen for creations, but you can typically mimic something like that by:

  1. Giving each document a createdAt timestamp field when you create it.
  2. Then query on that field in your listener, to only get documents where createdAt is after "now" (or whatever cutoff points your use-case requires).
  • Related