简体   繁体   中英

How can I make comment model with mongoose?

I'm making a website that uses mongoose, passportjs and 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. 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

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:

  1. PostRefId
  2. comment
  3. commentBy
  4. createdAt
  5. updatedAt
  6. isDeleted
  7. count so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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