Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting list of all posts with user like and total likes for each post in firestore

This question is related to querying firestore and best approach for implementing like system and counting no of likes for each post in firestore.

Let us say we have collection called posts, likes, comments, hashtags. Which has json structure like this.

posts:[{                   likes [{                      
  postId:id                postId:,                       
  createdAt:date           userId,                       
  title:string,            collectionType:'posts', 
  description,string       likedAt:date
  updatedAt,date          }]    
  createdUser:userId
  updatedUser:userId,    
}]

comments:[{              hashtags [{}]
  postId,
  userComments:[{
    userId:,
    comment:'',
    commentedAt:''      
    replies:[{}]
 }]
  1. What is best approach to implement like system with user comments along with nested replies for each comment in which improves efficiency and performance when we have millions of users and posts.

  2. Querying all posts with likes count on it and like whether logged in user like each post along with comments and comments count, fetching replies per comment if available.

  3. Is that my approach will work for firestore ? Or any best data modelling design also possible.

  4. Do we need to store hashtags in separate collection of in same collection i.e while create post, inside post object with hashtags array.

I'm newbie to firestore, I want to learn querying data in firestore. On basis of my knowledge in mongodb, I want to explore in firestore. Please help me in my problem case. Any suggestions or guidance is highly appreciated.

Thanks

like image 211
Bhargav Konkathi Avatar asked Dec 18 '22 01:12

Bhargav Konkathi


1 Answers

  1. I would suggest that you create a sub-collection of posts called comments and put the comments for each post, inside the post document. You can do the same with comments of comments. You can create a sub-collection of the comments sub-collection, also called comments. When it becomes possible to query for comments in all sub-collections, you'll easily be able to get all of this data in a single query.

  2. In your likes collection documents, I would change the userId field and replace it with createdUserId and likedUserId. Then you can query for all likes by a particular user's posts or all likes by a user.

  3. This is entirely a good fit for Cloud Firestore

  4. You should have a collection of hashtags. You will also need to store the hashtags used in a post, in a map of values, to allow querying.

like image 160
Jason Berryman Avatar answered Dec 27 '22 03:12

Jason Berryman