Do queries count as readings in Firestore? - firebase

If I have 1000 documents and I run a query to find 3 documents that meet a certain condition, will that count as reading 1000 or 3 documents?

Document reads are the fundamental unit of billing in Firestore as they relate to queries. Individual queries are not a primary unit of billing, but each query will be billed.
You will be billed for all the documents matched by a query, as those documents will be read and sent to the client. If your query matches no documents, then there is no billing. If your collection has 1000 documents, but your query returns 3 documents, you are charged 3 reads, not 1000.
The exception is that queries that return no documents are billed for a single document read. This means every query incurs a cost of at least one document read, no matter the results. If your query spans multiple requests (because of paging), you are billed at least one document read per request.
Please consult the documentation for Firestore billing to get more detailed information.

As per your query If you are fetching all documents at once on the client end and running your criteria search it will be counted as a single query.
However, if you use the query method of Firestore it will be also counted as a single query.
Note:
Don't have references yet, I am telling through the personal experience.

Related

Firestore get document download count?

Is there a way to determine a read count for each document in Firestore? I would like to limit read counts to 100,000 per document.
(2022-10-20) Edit:
Starting from now, counting the documents in a collection or the documents that are returned by a query is actually possible without the need for keeping a counter. So you can count the documents using the new [count()][1] method which:
Returns a query that counts the documents in the result set of this query.
This new feature was announced at this year's Firebase summit. Keep in mind that this feature doesn't read the actual documents. So according to the [official documentation][2]:
For aggregation queries such as count(), you are charged one document read for each batch of up to 1000 index entries matched by the query. For aggregation queries that match 0 index entries, there is a minimum charge of one document read.
For example, count() operations that match between 0 and 1000 index entries are billed for one document read. For A count() operation that matches 1500 index entries, you are billed 2 document reads.
Is there a way to determine a read count for each document in Firestore?
As also #FrankvanPuffelen mentioned in his answer, there is no API for doing that. If you need such a mechanism you need to create it yourself. That means that each time a user reads a document, you should increment a counter. That's pretty simple to implement since Firestore provides a really straightforward solution for that. To keep a counter for each read, you can increment a field in a document using ServerValue.increment(1).
Here are the docs for Android:
https://firebase.google.com/docs/database/android/read-and-write#atomic_server-side_increments
Here are the docs for iOS:
https://firebase.google.com/docs/database/ios/read-and-write#atomic_server-side_increments
And here are for the web:
https://firebase.google.com/docs/database/web/read-and-write#atomic_server-side_increments
There is nothing built into Firestore to limit the number of reads for a specific document. There is a quota system (which a.o. is used to enforce the quota on the free plan), but that doesn't apply per document.
You could do this through cloud functions with onRequest or onCall:
Read a value from Realtime database
If the value is larger than 0, return the respective document.
Then decrement the value in Realtime database
Sources:
https://firebase.google.com/docs/database/web/read-and-write#atomic_server-side_increments
https://firebase.google.com/docs/functions/callable

Get a collection of N documents cost with Firebase

Get 5 documents from users/ or from users/friends/Mike/sister/Jessica/rabbit... is the same amount of reads on the DB?
I mean, since I need to access to more locations will this increment the amount of reads or it depends only from how many documents I get from the collection?
You are charged only for documents that are returned to the client as a result of query, not for all the documents in the collection. If you want to limit your costs, you should also place a limit on the number of documents that the query can receive.

Firebase query charges additional reads?

How does firebase query works?
for example, if i write this query,
var collectionReference = FirebaseFirestore.instance
.collection('collection')
.where(cond)
.where(cond2)
.where(cond3);
So is this gonna return only the documents which fit the conditions?
AND I AM GOING TO GET CHARGED ONLY FOR THOSE DOCUMENT READS?
from docs TL:DR
Charges for reads have some nuances that you should keep in mind. The following sections explain these nuances in detail.
Listening to query results
Cloud Firestore allows you to listen to the results of a query and get realtime updates when the query results change.
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.
Managing large result sets
Cloud Firestore has several features to help you manage queries that return a large number of results:
Cursors, which allow you to resume a long-running query.
Page tokens, which help you paginate the query results.
Limits, which specify how many results to retrieve
Offsets, which allow you to skip a fixed number of documents.
There are no additional costs for using cursors, page tokens, and limits. In fact, these features can help you save money by reading only the documents that you actually need.
However, when you send a query that includes an offset, you are charged a read for each skipped document. For example, if your query uses an offset of 10, and the query returns 1 document, you are charged for 11 reads. Because of this additional cost, you should use cursors instead of offsets whenever possible.
Queries other than document reads
For queries other than document reads, such as a request for a list of collection IDs, you are billed for one document read. If fetching the complete set of results requires more than one request (for example, if you are using pagination), you are billed once per request.
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.

Does firestore charge for reads which does not return data

I checked the documentation on firebase but it does not mention the scenario where for example I have a collection with 100,000 records but the query that I am running does not bring back any result, which means none of the document satisfied the condition. Would I be still charged for checking 100,000 document ?
I currently have a cron job running in a node server which constantly queries the firestore database to look at records which have expired, it the record has expired (this is done by checking the timestamp with the current timestamp) then I am updating a field in the document. I noticed that I am being charged for the reads even though the result set was empty.
According to the Cloud Firestore billing:
“There is a minimum charge of one document read for each query that you perform, even if the query returns no results.”
All of your questions about Firestore billing should be made clear by reading the documentation. There are many different situations, and you'll possibly need to be aware of all of them, depending on your code.
But to briefly answer your question, you are only charged for documents that are actually delivered to the client, in the case of a simple query. The size of the collection is not considered at all for the purpose of counting documents read. Of course, if you have a large collection, you will increase the amount of billing based on its total storage size, including indexes.

Firestore Pricing - Does The Amount of Documents In a Collection Matters?

I have read in the documentation that I'm being charged for the amount of the requests I'm making to read, write or update documents. I have also read that reading a collection is priced the same as a reading a document ("For queries other than document reads, such as a request for a list of collection IDs, you are billed for one document read."), correct me if I'm wrong.
My question is: Does reading a collection with a big amount of documents in it (let's say - 10,000 documents) is priced the same as reading one with 10?
I'd like to get some explaination about it...
It depends on what you mean by "reading a collection", but for most people this means "querying a bunch of documents from a collection". And the answer is that the pricing generally depends on the number of documents retrieved.
To oversimplify things just a bit:
If you have a collection of 10 employees and you run a collection("employees").get() call, you will get back 10 employee documents, and be charged for 10 reads.
If you have a collection of 10,000 employees and you run a collection("employees").get() call, you will get back 10,000 employees, and be charged for 10,000 reads.
If you have a collection of 10,000 employees and you run a collection("employees").get().limit(10) call, you will get back 10 employees, and be charged for 10 reads.
If you have a collection of 10,000 employees, 4 of which are named "Courtney" and you run a collection("employees").where("first_name", "==", "Courtney") call, you will get back 4 employees and be charged for 4 reads.

Resources