Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore billing for reading a document with subcollections

I'm making an app where it stores how many minutes a user has studied with my app. My Firestore database starts with a "users" collection, and each user has their own document that is named by their userID generated in Auth.

My question is if I read their userID document, which has many documents in its sub collections, does that count as one read or does it also count the number of documents in the sub collections as well?

Thank You in advance.

like image 660
hiha2828 Avatar asked Jan 15 '19 01:01

hiha2828


2 Answers

If you are referring to is it 1 read to access a Document (in this case your generatedUserID) from FireStore?

I would imagine the answer would be yes.

Any query or read from Firestore only pulls the reference that you are mapping to. For example, if you grab the 3rd document in your User -> userID -> 3rd document, only the 3rd document will be returned. None of the other documents in that collection or any of the collections besides the userID.

Does that answer your question or are you asking something completely different?

For reference: https://firebase.google.com/docs/firestore/pricing#operations

Edit: Each individual Document that is pulled from the query will be charged. For example, if you pull the parent collection (with 6 documents in it), you will be charged for all 6 documents. The idea is to only grab the documents you need or use a cursor which let's you resume a long-running query. For example, if you only want the document pertaining to use data on a specific date (if your data is set up like that), you'd only retrieve that specific document and not retrieve all of the documents in the collection for the other days.

A simple way of thinking about it is: if you retrieve a document; anywhere, it counts as a read.

like image 184
Torewin Avatar answered Nov 05 '22 02:11

Torewin


The answer here from Torewin is mostly correct, but it missing one important detail. It says:

if you retrieve a document; anywhere, it counts as a read

This is not entirely true. Cached document reads are not billed as reads. This is one important feature of the Firestore client SDKs that helps lower billing costs. If you get a single document using the source option cache (options are "cache" or "server" or "default"), then the cache will be consulted first, and you get the document without billing. The cache is also used for query results when the app is offline.

The same is true for query results. If a document comes from cache for some reason, there is no billing for that read.

I am uncertain what Torewin means by this in comments: "They recommend you make multiple reads instead of 1 big one because you will save money that way". All reads are the same "size" in terms of billing, considering only the cost of the read itself. The size of the document matters only for the cost of internet egress usage, for which there is documentation on pricing.

It's worth noting that documents can't "contain" other documents. Documents are contained in collections or subcollections. These collection just have a "path" that describes where they live. A subcollection can exist without a "parent" document. When a document doesn't exist, but a collection is organized under it, the document ID is shown in italics in the console. When you delete a document using the client API, none of its subcollections are deleted. Deletes are said to be "shallow" in this respect.

like image 9
Doug Stevenson Avatar answered Nov 05 '22 02:11

Doug Stevenson