如何从mongoDB中删除填充的ID

编程入门 行业动态 更新时间:2024-10-07 15:25:08

如何从<a href=https://www.elefans.com/category/jswz/34/1771382.html style=mongoDB中删除填充的ID"/>

如何从mongoDB中删除填充的ID

我发表了一篇文章,然后使用要删除的评论和对该文章的评论的引用的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,
});

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

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 });
    }
  },
回答如下:

虽然使用请求注释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 });
    }
  },

更多推荐

如何从mongoDB中删除填充的ID

本文发布于:2024-05-07 21:44:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mongoDB   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!