11

With the advent of collection group queries, it isn't clear to me what benefit there is in using a root collection. In this article by the Firestore team, the only things that I can see is that there is a possibility of name collision, the security rules are slightly more complicated, and you have to manually create any query indices. Are there any other reasons to use a root collection and not subcollections / collection group queries?

Andrew
  • 6,295
  • 11
  • 56
  • 95

2 Answers2

16

When it comes to deciding, which is better to be used, a top-level collection or a sub-collection, please note that neither of the two is better than the other in terms of speed or billing. However, there are slight differences that can make you choose to use a top-level collection over a sub-collection. So the only differences might come from the way we are structuring the database, rather than performance. This is because Firestore is as fast as it is at level 1 is also at level 100.

Let's assume we have the following structure using subcollections:

Firestore-root
  |
  --- shops (collection)
       |
       --- $shopId (document)
              |
              --- products (sub-collection)
                    |
                    --- productId (document)

This schema can also be designed as:

Firestore-root
  |
  --- shops (collection)
  |    |
  |    --- $shopId (document)
  |
  --- products (collection)
       |
       --- $productId (document)
              |
              --- shopId: $shopId

When it comes to NoSQL databases, a recommended practice is to have the database flattened. Why? Because it looks more organized and each top-level collection contains only documents that are tied to the collection itself.

When it comes to sub-collections, when we look at the above example, each product is tied to the "products" sub-collection, $shopId, and "shops" collection.

Regarding security rules, the complexity comes with the number and diversity of filters you need to use. However, for top-level collections, it's a little easier.

In the end, bear in mind that when we are talking about architectures, we are always structuring a Firestore database according to the queries that we intend to perform. So it's up to you to choose between these two options and see which one of them can make your life easier.

Edit:

As also the docs state regarding structure Firestore data:

Root-level collections are good for many-to-many relationships and provide powerful querying within each collection.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • "You need to know at least the $shopId ahead of time" couldn't I use a collection group query on "products"? – Andrew Aug 10 '21 at 15:22
  • Yes, you can use a collection group query, but only if you need to get **all** products from **all** shops. If you want to get only the products that correspond to a single shop, you need to know its document ID ahead of time. [There are no wildcards in Firestore](https://stackoverflow.com/questions/53056717/query-on-wildcard-document-id). – Alex Mamo Aug 10 '21 at 16:24
  • If I wanted to get all of the products that correspond to a single shop, wouldn't I have to query by $shopId in the root collection as well? It seems like we would need $shopId in both the root collection and subcollection situation. – Andrew Aug 10 '21 at 16:29
  • 1
    Oh, yes. Just updated my answer, to not creating confusion. In conclusion, it's about the way you are structuring the database and not about performance. – Alex Mamo Aug 11 '21 at 06:01
  • Just to make sure I understand updated answer - the only reason to choose a root collection over subcollection is if you want simpler security rules? – Andrew Aug 11 '21 at 18:45
  • 1
    The only benefit is a clear structure, the possibility to create many-to-many relationships, and sometimes simpler security rules. That's it. Just updated my answer with the only reason why the documentation recommends root-level collections. Is it ok now? – Alex Mamo Aug 12 '21 at 11:01
1

It completely depends on your application structure and also on how often you are going to query in subcollections or querying the whole collection or structuring your data with private/public fields to grant access to specific users. However both approaches have their own tradeoffs like 1 write per second per document or 1mb size of the document etc. What I would suggest is to think of your queries first and to design your database to best handle the queries you need to perform. I would suggest to review the following documents on Data structure and Data model. If you wanna more about security I would suggest to have a look at the documentation on security.

  • This answer describes the benefits of using subcollections and subcollection group queries - I am looking for the opposite - what are the benefits of using a flat root collection. – Andrew Aug 09 '21 at 23:07