Mongoose schema for article
Modeling a separate schema for comment is not a good idea in my humble opinion, since it is a classic case of one to few mapping
which is an ideal use case for embedding the document. To give you a basic idea about data modeling i am quoting here
You need to consider two factors:
- Will the entities on the āNā side of the One-to-N ever need to stand alone?
- What is the cardinality of the relationship: is it one-to-few; one-to-many; or one-to-squillions?
Based on these factors, you can pick one of the three basic One-to-N schema designs:
- Embed the N side if the cardinality is one-to-few and there is no need to access the embedded object outside the context of the parent object
- Use an array of references to the N-side objects if the cardinality is one-to-many or if the N-side objects should stand alone for any reasons
- Use a reference to the One-side in the N-side objects if the cardinality is one-to-squillions
Please refer to a very well written and articulated post 6 Rules of Thumb for MongoDB Schema Design: Part 1 from mongodb blogs.
Even after this if you think it is a good idea to link to another schema please refer to this SO question - Referencing another schema in Mongoose
so I found a solution for this:
// :id is all articles with all idsrouter.post('/:id', function (req, res) { let comment = {}; comment.body = req.body.body; comment.user = req.user; comment.date = new Date(Date.now()).toDateString(); // Express validator req.checkBody('body').len(5, 100); let errors = []; errors = req.validationErrors(); if(errors) { Article.findById(req.params.id, function (err, article) { if(err)throw err; req.flash('danger', 'Body minimum length is 5 and maximum 100!'); res.redirect('/articles/'+article.id); }); } else { Article.findById(req.params.id, function (err, article) { if(err)throw err; article.comments.push({'body':comment.body,'user':comment.user,'date':comment.date}); article.save(function (err) { if (err) { throw err; }else { req.flash('success', 'Comment added!'); res.redirect('/articles/'+article.id); } }); }); }});
EDIT: code above in more readable form:
router.post('/:id', async (req, res) => { let article = await Article.findById(req.params.id); if (!article) res.status("403"); let articleUrl = "/articles/${article.id}"; let comment = { body: req.body.body, user: req.user, date: new Date(Date.now()).toDateString(); }; if (commment.body.lengh >= 100 || comment.body.length <= 5) { req.flash('danger', 'Body minimum length is 5 and maximum 100!'); return res.redirect(articleUrl); } articles.comments.push(comment); await article.save(); req.flash('success', 'Comment added!'); res.redirect(articleUrl);});