Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of using a root collection in Firestore vs. a subcollection?

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?

like image 302
Andrew Avatar asked Aug 05 '21 07:08

Andrew


People also ask

What is a Subcollection in firestore?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms.

What is the limited depth of Subcollection in firestore?

I saw on the Firebase Firestore documentation that the limits for the "Maximum depth of subcollections" is 100.

What is the difference between cloud firestore and realtime database?

Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database. Realtime Database is Firebase's original database.

How many documents can be stored in a collection firestore?

20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.

What are FireStore sub-collections and how do they work?

I’d like to talk at a high level about Firestore sub-collections. First, we need to review the Firestore data model. Firestore is a document/collection database. Documents hold whatever JSON data you’d like, but they must live within a collection. A document can have any number of sub-collections with their own documents.

What is the FireStore data model?

First, we need to review the Firestore data model. Firestore is a document/collection database. Documents hold whatever JSON data you’d like, but they must live within a collection. A document can have any number of sub-collections with their own documents. And you can keep nesting your data within sub-collections to your heart’s content.

What are the advantages and disadvantages of root level collection?

Create collections at the root level of your database to organize disparate data sets. Advantages: Root-level collections are good for many-to-many relationships and provide powerful querying within each collection. Limitations: Getting data that is naturally hierarchical might become increasingly complex as your database grows.

What are the advantages of FireStore?

Now, Let’s discuss the advantages of Firestore. Through one of the essential features of a firestore, data can be retrieved from mobile and web in real-time. When a query is performed one can attach a listener to it that receives real-time callbacks, hence query listener will be notified every time it changes.


Video Answer


1 Answers

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.

like image 59
Alex Mamo Avatar answered Nov 15 '22 11:11

Alex Mamo