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:[{}]
}]
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.
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.
Is that my approach will work for firestore ? Or any best data modelling design also possible.
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
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.
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.
This is entirely a good fit for Cloud Firestore
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With