2

I'm building a site where users can post a short text and others can comment on it. I'm using Firebase Authentication to manage users and Firebase Firestore to store the posts.

I have 2 questions:

1st, to store the comments, should I use root level collection? Or sub-collection under the documents of post?

2nd, On my site, users are allowed to change their display name (using Firebase Auth). How can I ensure that the comments and posts will show the updated display name?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

There is no singular correct answer for #1, it depends on the use-cases you want to implement. The most important thing to realize is that you can't query across multiple collections. So if you ever want to show/query comments on multiple posts, you will need to ensure all comments are in a single collection.

On the other hand, if you only ever show/query comments for a single post at a time, it is probably easier if you keep the comments in a subcollection of the post they belong to.

For #2, I'll refer you to a previous answer I gave on How to write denormalized data in Firebase. While that question was about the realtime database, the general approaches apply equally to Cloud Firestore.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807