1

So I have read all the official documentation regarding Firestore and I understand all the basics and most of the advanced topics. But in the end I came up with two questions:

  • Is there any advantage of using Subcollections over Root-level collections? Well, the database may be a little bit more organized if it is structured in Subcollections. But in the end, regarding performance and scalability, it seems that Root-level collecitons are just superior. All the advantages listed in the official documentation also apply to Root-level collections, or am I misunderstanding something?

  • Let's take the famous chat example and consider a lot of chat groups (like WhatsApp). Whenever a user opens a chat, the user can see information about the chat and the members of this chat. But this is where my second question comes up: Wouldn't showing the members of the chat mean, that whenever someone opens the chat, another collection containing said users profile image and name will be loaded? If you have a 'WhatsApp' group with 10 people in it, this would mean 11 reads (one for the group itself, 10 for the image/name of the members) whenever anyone opens the group. Let's extend that idea and make groups publicy available, which would result in an insane amount of reads, just to see basic information of the group. Since Firestore charges for collection reads, this will blow up very fast, wouldn't it?

Thomas
  • 2,375
  • 2
  • 17
  • 32

1 Answers1

1

You can chain in depth up to a maximum of 100 subcollections. As per the offial documentation regarding usage and limits.

Maximum depth of subcollections: 100

Regarding your question:

Is there any advantage of using Subcollections over Root-level collections?

As far as I know, Firestore can as quickly look up a node at level 1 as it can at level 100. So for a regular database, depth should not be a factor that affects speed on a technical level. IMHO, it's best to have the data as flatten as you can.

Regarding your second question, you can store either objects or just references but before taking a decision, I recommend you to be aware of some details that can be found in my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for the answer. The maximum depth of subcollections is irrelevant anyways if I try to go as flat as possible, so in your opinion Root-level collections are better? The only disadvantage I could possibly come up with, is that it could maybe require more indexes. And regarding the second question: This means that this use case will probably be expensive and I can choose if I want it to be expensive regarding performance or cost? – Thomas Oct 31 '18 at 14:47
  • When using top-level collections, your database can be better organised. Yes, I recommend using root-level collections. Even if you are using nested collections or top-level collections, Cloud Firestore automatically creates indexes on individual fields of your documents. So it doesn't matter which approach you take. If you need complex indexes that span multiple fields, you can create them manually in the Firebase console. Regarding your second question, yes, that's correct. – Alex Mamo Oct 31 '18 at 14:54
  • Well what I meant is, that on the top-level I will generally need more indexes. Let's say I want every group, a user is in. If I do it with a top level collection i need to index [userId,groupId], within my "Membership" root-collection. If I do a subcollection of "User" I end up with reference.collection("users").document().collection("memberships"). In that case I don't need the compund index I would have on top-level. But as I said this is literally the only advantage I can come up with. – Thomas Oct 31 '18 at 15:23