首页 > 评论系统怎么设计比较好?

评论系统怎么设计比较好?

带这种@功能的评论


第一种方式:
评论表

id user_id comment
1 2 zhis is a comment

回复表

id user_id comment_id reply
1 2 1 zhis is a reply

先遍历评论

select * from comment left join user on comment.user_id = user.id.

再遍历回复

select * from reply where comment_id left join user on user.id = reply.user_id where reply.comment_id = comment.id

合并后以时间排序

方法二:
增加一个回复给谁的字段

id user_id reply_comment_id comment
1 2 20 zhis is a reply

如果reply_comment_id是NULL的话;没有@,是数值的话,取出那条评论的用户,增加@


我觉的这个设计可以用mongodb,每条记录如果存在回复则保存该被回复记录的_id。
查询的时候将数据populate出来,这里的难点主要是回复的嵌套,可以考虑用deep-populate的插件,比如:
https://github.com/buunguyen/mongoose-deep-populate

【热门文章】
【热门文章】