0

For a small app prototype I'm building, I would like to use firestore (firebase) to store some data. I'm wondering if the following is a good way of going about a nosql database.

I have Paths, that belong to Categories. A Path can have courses and comments. I would like users to see the likes a path has, and get categories sorted by the amount of paths inside.

That's why I'm adding the paths_count on the categories table, I will use Cloud Functions to update the counts for the likes and paths on each database update.

categories: [
    1: {name: "productivity", paths_count: 10},
    2: {name: "cooking", paths_count: 5},
]

paths: [
    1: {
        name: "Productivity 101",
        category_id: 1,
        likes_count: 5,
        likes: [],
        courses: [],
        comments: []
       }
]

Is this a good start?

Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
  • 2
    If your question is whether it's a good idea to add a count property to ease querying on counts, then the answer is "yes". Also see this solution in the documentation for a scalable way to keep such counters up to date: https://firebase.google.com/docs/firestore/solutions/aggregation (you might not need that yet, but it's a good thing to keep in mind as you further build your app). – Frank van Puffelen Nov 28 '18 at 03:26
  • Thanks Frank! I was also wondering if the structure of the courses and comments inside of the paths is okay, since they will always be queried with the parent. Thank you – Miguel Stevens Nov 28 '18 at 04:17
  • Please also check the last part of my answer from this **[post](https://stackoverflow.com/questions/48534676/how-to-count-the-number-of-documents-under-a-collection-in-firestore/48540276#48540276)**. – Alex Mamo Nov 28 '18 at 11:05
  • It indeed all depends on your app, and your willingness to write more code vs to read too much data. There is a no single proper solution. I recommend reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and watching [Getting to know Cloud Firestore](https://www.youtube.com/playlist?list=PLl-K7zZEsYLluG5MCVEzXAQ7ACZBCuZgZ). – Frank van Puffelen Nov 28 '18 at 14:10

0 Answers0