When querying a deeply nested sub-collection, do you get charged for reading the documents at the top of the hierarchy or do you just get charged for the documents that belong to the sub-collection that is being read.
You get charged for the documents that the server needs to read for you. If you only read documents from the subcollection, you will only get charged for those. There is no implicit read-from-top-level-collecton required for reading from a subcollection.
Related
For instance does retrieving a document which is in the sub collection of another document count as 1 read or 2?
A get of a document always costs one read. It doesn't matter if it's nested under any other documents. The other parent documents are not read.
I have a root collection of products that contains 10 documents.
Each document has a subcollection of 100 documents and no other fields.
Now if I read the documents of the products collection by
db.collection("products").get();
how many reads will be charged, Since I'm getting documents of products collection having 10 documents I will be charged for 10 reads? or each document has a subcollection of 100 documents, so I will be charged for 10*100 =1000 document reads?
In Firebase Firestore you will be charged only for data you receive.
When reading a collection you won't get the subcollections with it.
That means if you have 10 produst with 100 items in theire subcollections. You will be charged only for those 10 products you get because you won't get the subcollections with that request.
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.
I have a question about Reads/Write in a Firestore DB.
The scenario is:
I have a Collection "City" (for example 20 Cities) and it has a subcollection "Restaurants" (e.g 500 restaurants):
Now is my question: When I want to get all Restaurant in a City, how many reads would Firestore bill? 500?
And when I want to add a Restaurant: Would it only need 1 write to add this document to the subcollection?
As Andres said: you are charged for document reads and writes. It doesn't matter what collection or subcollection the document comes from, each time the server reads a document on your behalf, you're charged for that document read.
So if you read 500 restaurant documents out of the subcollection of a city, you'll be charged for 500 document reads. If you add a single document to that subcollection, you're charged for a single document write.
If you regularly find yourself reading the same set of documents (e.g. the same 500 restaurants for all users in that city), consider creating a data model that reduces the number of documents you need to read. For example: you'll probably need a subset of the information from each restaurant, so you could extract that for all restaurants in the city into a "top restaurants list" document. This type of data duplication is quite normal in NoSQL databases, and key to keeping great performance with a reasonable cost.
Also see:
Getting to know Cloud Firestore, which covers this and many more scenarios.
NoSQL data modeling, which covers general data modeling for all kinds of NoSQL databases.
This answer I gave earlier today: Maxing out document storage in Firestore
One document read/write always costs the same, it could be in a collection, or a subcollection, or a subcollection of a subcollection - or a sub of sub of sub... you got the idea :)
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.