7

This may sound silly but I'm a little bit confused with Firestore pricing.

I've read other questions here, read the doc and watched a few videos.

What does count as a read operation?

.get() or .data();

I tried figuring out myself by viewing the quota using and playing with Postman, but the read operation count is not increasing.

I'm using Node SDK.

Thanks

Faheem
  • 1,105
  • 11
  • 28

1 Answers1

10

From the offical documentation regarding listening to query results:

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated.

The act of listening does not itself count as a read, however there is a minimum of one document charged per query. From the pricing page, under "Minimum charge for queries":

There is a minimum charge of one document read for each query that you perform, even if the query returns no results.

If you are about to call .data() it means that the document that you are looking for exist in the database and you are already inside the callback. With other words, the .get() call was already performed and you are already charged with a read operation.

Please also note that, if you re-listen a short while after you've already done so, you won't get charged for documents that haven't changed since you listened last.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    In that case, if I've used a query, such as ".where().limit(10).get()", I've already been charged for 10 docs. – Faheem Oct 29 '18 at 12:50
  • 1
    Yes, that's correct. You are charged with only 10 read operations because you have used `limit(10)` method. – Alex Mamo Oct 30 '18 at 02:49
  • If am getting the total number of count from a collection, will that be considered as read operation? @AlexMamo – Mac_Play Apr 22 '19 at 07:21
  • Regarding counting documents, please check **[this](https://stackoverflow.com/questions/48534676/how-to-count-the-number-of-documents-under-a-collection-in-firestore/48540276#48540276)** out. – Alex Mamo Apr 22 '19 at 07:23
  • Thanks for the link. But that's for java, am looking for swift. Please help. @AlexMamo – Mac_Play Apr 22 '19 at 08:19
  • But a .data() counts as another read? Or it's just the .get()? Because I use a .get() and inside that I get many .data()'s – Dor Furman Nov 29 '21 at 19:48
  • 1
    @DorFurman When you call .data(), it means that want to read the result, so you'll be charged. – Alex Mamo Nov 30 '21 at 06:05
  • @AlexMamo that would mean you get charged twice. data() being a synchronous call that immediately returns the result, makes me think, that it would never access Firestore, therefore should not count as a read operation. Whereas get() returns a Promise that waits for Firestore to return the document. If data() counted as a read, you'd have to create your own object to pass it to methods that need id and data instead of simply using the document reference. – dfinki Jul 20 '22 at 14:50
  • @dfinki When you call `get()`, the request might succeed or can be rejected by the Firebase servers. When `data()` fires, it means that the request is successful and you get a result. According to the number of documents you get, you're charged with one read operation for each one of the documents. – Alex Mamo Jul 20 '22 at 15:21
  • Don't we need to get a documentSnap to call data() in the first place? Above I meant a documentSnap and not the DocumentReference. I always think getting a documentSnap counts as a read. docRef = db.doc("collection/someId"); docSnap = await getDoc(db.doc("collection/someId")); docData = docSnap.data(); – dfinki Jul 21 '22 at 08:51
  • @dfinki Yes, we need that documentSnap to call data(). And yes, that's the expected behavior, you are charged with reads since you're trying to read the data that exists in that object. – Alex Mamo Jul 21 '22 at 09:00