11
votes

If a document in Cloud Firestore has a subcollection, are the documents in those subcollection included in the parent document's size? For example, if I have data like:

rootCollection/
    parentDocument:
        field1: "value1"
        field2: "value2"
        ...
        subCollection/
           childDocument:
                anotherField: "OfGreen" 
                ...

is the size of childDocument included in the size of parentDocument

From the storage size calculation documentation, it doesn't sound like the contents of subcollections are included in the parent document sizes, but I just wanted to clarify.

Thanks!

1
Can you give an example of where this answer would matter?Frank van Puffelen
@FrankvanPuffelen As I understand it, the maximum document size is 1MB. If rootCollection is chatRooms/, and parentDocument has a subcollection of chatData, 100's of chatRooms * 1000's of chat messages could add up. I'm not saying I have this problem, I just want to understand better.ZenPylon
Ah in that case: that limit applies to an individual document, excluding its subcollections (that would be a pretty unrealistic limit). Let me know if it turns out different in your tests.Frank van Puffelen
Great, thanks for the clarification! I don't anticipate any problems, but I'll definitely bring it up if I run into an issue.ZenPylon

1 Answers

23
votes

It is already stated by @frank-van-puffelen however I would like re-state and share the official references for further emphasis.

TL;DR;

Size of the subcollection doesn't affect the parent document's size in any way.

References

Firestore's Choose a Data Structure document is a very clear and prompt reference for comparing the design alternatives. On this page under the section Subcollections, it is stated that:

Advantages: As your lists grow, the size of the parent document doesn't change. You also get full query capabilities on subcollections.

Moreover, on the Working with Arrays, Lists, and Sets documentation under the Limitations section, it is emphasized that:

Document size - Cloud Firestore is optimized for small documents and enforces a 1MB size limit on documents. If your array can expand arbitrarily, it is better to use a subcollection, which has better scaling performance.

And lastly, although it is not directly related with the question, I would like to use the chance to mention it here, because it yield me to the correct documentation pages when I got the same question as the OP: ecgan/firestore-considerations is a short and remarkable list on Github which tries to summarize the caveats and before-hand considerations for Firestore.