0

Coming from an RDMS background I am trying to understand NoSQL databases and planning a simple project that includes topics, posts & comments.

Topics have posts & posts have comments

I have found the following guide that suggests using the following top-level collections:

  1. A users collection
  2. A posts collection
  3. A user-posts collection
  4. A posts-comments collection

https://firebaseopensource.com/projects/firebase/quickstart-android/database/readme/

I fail to understand the benefits of (3) above as surely we can simply filter (2) based on the user, even 3 would still need to be filtered.

What is the logic of having comments as a top-level collection as opposed to having comments as a subcollection under posts? Is this not the better way to store hierarchical data?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Dercni
  • 1,216
  • 3
  • 18
  • 38
  • Two collections 1. topics and 2. users then the topics collection will have sub-collections of posts and each post should have a sub-collection of comments. You can also index the sub-collection depending on your filters and can do whatever you like. Please let me know issues with this model – Sulman Azhar Sep 30 '22 at 10:23

1 Answers1

2

In the NoSQL world, we are structuring a database according to the queries that we want to perform.

What is the logic of having comments as a top-level collection as opposed to having comments as a subcollection under posts?

None is better than the other. However, there are some differences:

Is this not the better way to store hierarchical data?

There is no "perfect", "the best" or "the correct" solution for structuring a Cloud Firestore database. We always choose to create a structure for our database that satisfies our queries. So in your case, I would create a schema that looks like this:

Firestore-root
  |
  --- users (collection)
  |    |
  |    --- $uid (document)
  |         |
  |         --- //user fields.
  |
  --- posts (collection)
       |
       --- $postId (document)
            |
            --- uid: "veryLongUid"
            |
            --- //post fields.
            |
            --- comments (sub-collection)
                  |
                  --- $commentId (document)
                         |
                         --- uid: "veryLongUid"
                         |
                         --- //comment fields.

Using this schema you can:

  • Get all users.
  • Get all posts in the database.
  • Get all posts that correspond to only a particular user.
  • Get all comments of all posts, of all users in the database. Requires a collection group query.
  • Get all comments on all posts that correspond to a particular user. Requires a collection group query.
  • Get all comments of all users that correspond to a particular post.
  • Get all comments of a particular user that correspond to a particular post.

Am I missing something?

If you think that all the comments of a post might fit into 1 MiB maximum limitation, then you should consider adding all comments into an array. If not, I highly recommend you read the following approach:

Where I have explained how can we store up to billions of comments and replies in Firestore.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • so topics would be a top-level collection with posts & comments as subcollections? – Dercni Sep 30 '22 at 12:30
  • 1
    If you can query and you can get the desired results, why not? It could be like that. – Alex Mamo Sep 30 '22 at 12:31
  • Great doc above. I think I may cache the top 10 or so comments in an array in the posts doc. Using subcollections would it be easy to find the post with the most comments or most popular hashtags? – Dercni Sep 30 '22 at 21:20
  • 1
    Cacheing 10 comments in an array, sounds like a good idea, and it will help you save some reads. Regarding your last question, not really. When it comes to counters, for example, post with the most comments, then you consider storing that number as property in document, rather than [counting all documents](https://medium.com/firebase-tips-tricks/how-to-count-the-number-of-documents-in-a-firestore-collection-3bd0c719978f) each time. – Alex Mamo Oct 01 '22 at 08:39
  • So can I help you with other information? – Alex Mamo Oct 01 '22 at 09:03