mongoose update attribute code example
Example 1: mongoose save or update
findByIdAndUpdate(_id, { something: 'updated' }, { upsert: true });
Example 2: mongoose update subdocument by id
BlogPost.findById(req.params.postId, function (err, post) {
var subDoc = post.comments.id(req.params.commentId);
subDoc = req.body;
post.save(function (err) {
if (err) return res.status(500).send(err);
res.send(post);
});
});
BlogPost.findOneAndUpdate({_id: req.params.postId}, {$push:{ subDoc: req.body }}, (err, doc) => {
});