Comments via embedded documents within Post schema#7
Comments via embedded documents within Post schema#77MinutesDead-Git wants to merge 5 commits into100devs:mainfrom
Conversation
- Move Return To Feed button above feed, as the button would be hard to find after the user had many posts.
- Add comment post route based on post id. - Add commentPost() method to posts controller. - Add userName and comments fields to Post schema. - Add comments section and input form to post view. - Add userName (author) display to each post in public feed.
- Add Comment schema as embedded document within Post schema (since comments will only ever be associated with a particular post). - Add replies to comment schema, as embedded array of comment schemas (recursive "this"), e.g., each reply is also a Comment. - Add port to server console on startup.
| type: Array, | ||
| required: false | ||
| }, | ||
| comments: [Comments.schema], |
There was a problem hiding this comment.
This is where you can add Comments Schema as an embedded document to the Post Schema, to save yourself from additional queries after first querying for your Post.
You automatically get the Comments associated with that Post, just by querying the Post.
| default: Date.now, | ||
| }, | ||
| replies: { | ||
| type: [this], |
There was a problem hiding this comment.
Here is where you can recursively add Comment replies as arrays of Comments (who will then each have their own Comment replies array), by referencing "this".
|
Adjusted some of the schema approach after stream. I think it's better to do comments as an array of embedded documents, rather than having a whole other collection of comments, since comments are only ever contained within a specific post. This means you can make one query for the post as usual and you automatically get the comments associated with it, rather than needing to do two separate queries for the post and then the comments with a matching post ID. Check the comments above for examples |
Check it out! Very simple comments implementation.
Also adds user name display to each post in the public feed.