asanas
asanas

Reputation: 4280

Modelling data for a Google Firestore project

We are creating a new app, which has the following types of data and I am looking at some help with the best way to organize data in Firestore.

We have the following:

Upto here, it's straight forward, as we can have one collection for users and one for Products.

Now, the next thing is this:

  1. Each product can have reviews (Many reviews by users)
  2. Each review itself can have many user comments
  3. Each product can have its own comments by users.

So, a typical product will look like this:

Product (Posted by a user) - Reviews by users - Comments on reviews by users - Comments on product by users

I am looking at the best way to organize this data in Firestore.

I could think of two possible ways:

Possibility 1

Have 4 collections:

The documents in these collections will be referenced by product ids.

Possibility 2

Have two main collections and then have reviews and comments as sub-collections to product collection:

Which of these two possibilities would be the best way to organize content in Firestore?

Upvotes: 1

Views: 572

Answers (1)

Mark Kvetny
Mark Kvetny

Reputation: 674

the firestore docs offer a nice list of pros and cons for both approaches: https://firebase.google.com/docs/firestore/manage-data/structure-data

TL;DR having root-level collections is preferable most of the time unless data is really hierarchical, so I'd go with 1) as it just offers you the most flexibility.

For example you still have the ability of filtering over all comments, so if you want to display all comments by a user you could do that easily while with 2) such a task is way harder to accomplish.

Upvotes: 2

Related Questions