3
votes

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

1

1 Answers

4
votes

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.