如何从mongoDB删除填充的ID

时间:2020-05-20 16:17:17

标签: node.js reactjs mongodb express

我发表了一篇文章,然后使用populate方法将评论填充到该帖子中,我想删除该评论以及该帖子中评论的引用,我可以删除该评论,但是它不想删除删除

const { ObjectID } = require("mongodb");
    const { Comment, Post } = require("../models/User-Post");
    module.exports = commentControlleur = {
      addComment: async (req, res) => {
        const userId = ObjectID(req.params.userId);
        const postId = ObjectID(req.params.postId);
        const { body, date } = req.body;
        try {
          const newComment = new Comment({
            body,
            date,
            postId,
            userId,
          });
          try {
            Comment.create(newComment, (err, doc) => {
              if (err) res.status(503).json({ errors: err });
              else {
                Post.findByIdAndUpdate(
                  postId,
                  { $push: { comments: doc } },
                  { new: true },
                  (err, data) => {
                    if (err) res.status(504).json({ errors: err });
                    else {
                      res.status(200).json(newComment);
                    }
                  }
                );
              }
            });
          } catch (error) {
            res.status(500).json({ errors: error });
          }
        } catch (error) {
          res.status(501).json({ errors: error });
        }
      },

这是我在帖子架构中添加评论的方式

const postSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
  },
  title: String,
  date: String,
  body: String,
  postType: String,
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "comment",
      autopopulate: true,
    },
  ],
});

const commentSchema = new mongoose.Schema({
  postId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "post",
  },
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
  },
  body: String,
  date: String,
});

但是当我删除评论时,帖子中评论中的ref不会删除,这就是我编写删除代码的方式

deleteComment: async (req, res) => {
    const postId = ObjectID(req.params.postId);
    const { id } = req.body;
    try {
      const searchDeleteCommment = await Comment.findOneAndDelete({ _id: id });
      if (searchDeleteCommment)
        Post.update(
          {_id: postId }, 
          { $pull: {comments: id} }
          );
      res.status(200).json({ msg: "comment deleted" });
    } catch (error) {
      res.status(501).json({ errors: error });
    }
  },

1 个答案:

答案 0 :(得分:0)

使用请求注释ID时应采用ObjectID格式

deleteComment: async (req, res) => {
    const postId = ObjectID(req.params.postId);
    const { id } = req.body;
    try {
      const searchDeleteCommment = await Comment.findOneAndDelete({ _id: id });
      if (searchDeleteCommment)
        await Post.update(
          {_id: postId }, 
          { $pull: {comments: ObjectID(id)} }
          );
      res.status(200).json({ msg: "comment deleted" });
    } catch (error) {
      res.status(501).json({ errors: error });
    }
  },