Answer the question
In order to leave comments, you need to log in
How to add new object to MongoDB array?
There is such a scheme:
var PostSchema = new Schema({
title: String,
description: String,
content: String,
image: String,
category: String,
url: String,
views: {
type: Number,
default: 0
},
comments: [
{
username: String,
email: String,
comment: String
}
]
});
router.put('/blog/:url/add-comment', (req, res) => {
let db = req.db
Post.updateOne({ 'url': req.params.url }, {$push: {comments: {
'username': req.body.comments.username,
'email': req.body.comments.email,
'comment': req.body.comments.comment
}}})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question