📜  猫鼬按 id 更新子文档 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:27.848000             🧑  作者: Mango

代码示例1
//this method for add data to subdocument
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);
    });
});

// alternative second method you can use this
BlogPost.findOneAndUpdate({_id: req.params.postId}, {$push:{ subDoc: req.body }}, (err, doc) => {
      // do something here
});