5

Assume that i have 20 documents in a collection, and each document has a field that contains the number of views for querying. Example below

collection
     |_document
             |_views: 100
     |_document
             |_views: 600
     |_document
             |_views: 10

Later i query the documents to return top 5 highest views. Now my question is, will i be charged for 20 document reads or 5 document reads

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Jama Mohamed
  • 3,057
  • 4
  • 29
  • 43

1 Answers1

5

This Firestore documentation item (Section "Listening to query results") will give you the answer:

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. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)

Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.

In other words, if your query limits the number of documents returned to 5 (e.g. with limit(), as explained here), you will only pay for 5 document reads (unless you are using a listener -and not get()- and a document is created/modified/deleted and changes the top 5 or unless you encounter the 30 minutes disconnection specific case mentioned above).

On the other hand, if you filter the top 5 docs on the client-side, after receiving the entire set of documents, you will obviously be charged for the full set.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • 1
    So what you are saying is, even if i have 200 documents and the query goes through all the 200 document and i limit to 5 (not done on the client-side), i only get charged for the 5 reads? – Jama Mohamed Aug 26 '18 at 13:18
  • Yes, indeed. If for example you do a query like `documentsCollectionRef.orderBy("views", "desc").limit(5).get().then(...)` you will pay for 5 reads. – Renaud Tarnec Aug 26 '18 at 14:28
  • @RenaudTarnec Can you give us a link where you've found this explicitly written? I have a case where I must find a single document in 50k document collection, does that mean that I'll be charged only for a single request? Thanks! – Sebastijan Dumančić Jan 19 '19 at 12:34
  • 1
    @SebastijanDumančić If you make a Firestore query to get this doc, then yes you will be charged for one read only. Have a look at the official following video https://youtu.be/6NegFl9p_sE at 2:20. It is the video that is embedded in the doc I refer to in my answer. (If, on the other hand, you do the filter on the front end then you would pay for the entire set of docs. Of course, this is not recommended, for price and also for performance) – Renaud Tarnec Jan 19 '19 at 16:21
  • @RenaudTarnec Thanks, I'll look into it a bit more. – Sebastijan Dumančić Jan 19 '19 at 16:26