简体   繁体   English

如何用猫鼬制作评论模型?

[英]How can I make comment model with mongoose?

I'm making a website that uses mongoose, passportjs and nodejs.我正在制作一个使用猫鼬、passportjs 和 nodejs 的网站。 I want to add a comment functionality but I don't know how to make the comment model.我想添加评论功能,但我不知道如何制作评论模型。

I already created my photos model, user model(this model works with passportjs) and comment model.我已经创建了我的照片模型、用户模型(该模型适用于passportjs)和评论模型。 But then I didn't know what to do.但当时我不知道该怎么办。

I want user to write their own comment which my users can and when adds a comment I want to show username automatically on comments section with 'req.user.username' but I want this username to be saved in database我希望用户写他们自己的评论,我的用户可以,当添加评论时,我想在评论部分自动显示用户名,并带有“req.user.username”,但我希望将此用户名保存在数据库中

You must have a separate model for comments.您必须有一个单独的评论模型。 Reason being, Comment count may go up to thousands and even more than that, if you maintain an array, it will not be easy to maintain and will be slow as well.原因是,评论数可能会达到数千甚至更多,如果你维护一个数组,它会不容易维护,也会很慢。

Even, if you create a new model for comment, it will help you in pagination as well.甚至,如果您创建一个新模型进行评论,它也会帮助您进行分页。 Your commentSchema model may contain:您的commentSchema模型可能包含:

  1. PostRefId PostRefId
  2. comment评论
  3. commentBy评论者
  4. createdAt创建于
  5. updatedAt更新时间
  6. isDeleted已删除
  7. count so on.等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM