从猫鼬中删除引用

时间:2019-03-01 17:23:52

标签: javascript node.js mongodb express mongoose

我有一个带营地和评论的mongo数据库。 在露营地模型中,我引用了以下注释:

comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Comment"
    }

我有一条删除评论的删除路线:

router.delete("/:comment_id", middleware.checkCommentOwnership, (req, res) => {
  Comment.findByIdAndRemove(req.params.comment_id, err => {
    if (err) {
      res.redirect("back")
    } else {
      req.flash("success", "Comment deleted");
      res.redirect("/campgrounds/" + req.params.id)
    }
  });
});

但是,我注意到,在露营地集合中仍然存在对不存在的注释的引用。

问题:我需要在删除评论路线中添加些什么,以从露营地集合中删除参考?

P.S .:这个问题不是重复的。我需要相反的事情。我不需要删除带有所有引用的注释的Camp,而是在删除注释后删除Campground内部注释的引用。

0 个答案:

没有答案