1
votes

I currently have a collection of documents in firestore. Each of these documents holds an array of json objects. I believe it would be better to store these arrays as sub collections in each document. My only concern is the pricing aspect of reading the sub collection in.

As its just currently an array on each document I believe this counts only as one read (correct me if im wrong) when i fetch a document.

If i move to using a sub collection and read the entire collection with the code below, does this count as one read or multiple? I fear this could be expensive.

db.collection("cities").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " => ", doc.data());
    });
});

https://firebase.google.com/docs/firestore/query-data/get-data

Thanks for your help :)

1

1 Answers

3
votes

Reading one document counts as one read, regardless of how big the document is. There's actually no such thing as JSON in a document, firebase will flatten your structure in the background, it just looks like JSON to you. Image your document has a key person.

Now the person object could look like that

{
  name: "Phil",
  age: 25
}

Firestore will save all fields individually, so technically your document now has the fields person.name and person.age instead of just a person field. What that means for you is that even if you have complex objects inside of a single document, it's still only one document and therefore counts as one read.

Loading subcollections will count as a separate read. But imagine instead of a small object like in my person example you have objects with sizes of multiple kilobytes or even megabytes. Not only will you fetch a huge payload every time you query a document, where you probably only need a few attributes of, your bill will also increase due to network egress, so that one additional read will be worth it.

The question wether to use subcollections or not comes down to how big your document might get. But that's up to you to decide.

Edit: For the use case you've described in your comment it would probably a good idea to store the comments both in the document itself as well as in a subcollection. For example, your document could hold the top 5 comments directly, so that your network egress stays low, but you still have access to the most important comments instantly. Then, if you want to load more comments, you could query the subcollection for the full collection of comments. In NoSQL databases, redundant data is allowed and sometimes actually good.

Also I recommend firebase's video on this topic: https://www.youtube.com/watch?v=o7d5Zeic63s