I have a question because I don't want a $30k bill.
Is this an acceptable code to work with, or is there a better way?
I am using firebase 8.10.0 with Vue.js 2.6.11 and I created the index in firestore.
If there are 1000 documents in the collection "users" and I need to apply filters in the query before making the request for only 50 using this code:
let users = db.collection("users");
users
.where("country" "==" "uk")
.where("online" "==" true)
.where("status" "==" "play")
.orderBy("time", "asc")
.startAt(startTime)
.endAt(endTime)
.limit(50)
.get().then(() => { .... });
Will I be charged for 50 readings or 1000 readings or how much?
What do they mean by "Cursors" in Understand Cloud Firestore billing?
CodePudding user response:
You are changed for the number of documents that need to be read from the server. Since you only read at most 50 documents, you will never be charged for more than 50 document reads with this query. No matter if the collection has 50 documents, or 50 million documents, the charge for this query will be no more than 50 document reads.
The only exception to this general rule is if the query has no results. In that case you will be charged one document read.
CodePudding user response:
Interesting topic/question. Unfortunately, I cannot exactly answer how the reads are billed.
Have you already used the SO search function? There are a lot of posts listed that could help to answer your question, see https://stackoverflow.com/search?q=Firestore billing
I just read the Understand Cloud Firestore billing article you have linked which states that
- cursors allow you to resume a long-running query
- offsets allow you to skip a fixed number of documents to save money
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.
I am guessing here: Reading your code makes me think that you're billed for 50 due to .limit(50)
that is limiting the query result, right? The other options startAt(startTime)
and endAt(endTime)
could influence the count (due to runtime), couldn't they?
I second the statement in code's comment above. Use the feedback function to tell them that you need an example and that their documentation is unclear in this regard. Later, you can maybe add their feedback to inform others.