问题
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
回答1:
I would suggest that you create a sub-collection of
posts
calledcomments
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 thecomments
sub-collection, also calledcomments
. 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 theuserId
field and replace it withcreatedUserId
andlikedUserId
. 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.
来源:https://stackoverflow.com/questions/48820787/getting-list-of-all-posts-with-user-like-and-total-likes-for-each-post-in-firest