【问题标题】:Node.js Mongoose - model in model updateNode.js Mongoose - 模型更新中的模型
【发布时间】:2020-12-21 06:09:51
【问题描述】:

我有 1 个博客帖子模型:

const blogSchema= new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  name: {
    type: String,
    required: true,
    max: 50,
    min: 6,
  },
  description: {
    type: String,
    required: true,
    max: 1024,
    min: 25,
  },
  date: {
    type: Date,
    default: Date.now,
  },
  comment: [commentSchema],
});

这里的重要部分是最后一个字段comment。为此,我有另一个架构:

    const commentSchema = new mongoose.Schema({
      date: {
        type: Date,
        default: Date.now,
      },
      comment: {
        type: String,
        max: 1024,
        min: 5,
      },
      userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
      },
    });

通常在创建帖子时,可以添加 cmets。在单独的文件 User.js 中,我有用户模型:

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    unique: true,
    required: true,
    max: 255,
    min: 6,
  },
  email: {
    type: String,
    unique: true,
    required: true,
  },

  password: {
    type: String,
    required: true,
    min: 6,
  },
  date: {
    type: Date,
    default: Date.now,
  },
});

我如何将这些连接起来一起工作。我有所有帖子都可见的端点(无论是哪个用户提供的)。我希望 1 个用户能够评论另一个用户的帖子,并且他的名字出现在它下面。

所以任何想法我可以如何创建:

  1. 将 cmets 存储在 blogSchema 的评论字段中的端点
  2. 用户将被记录在commentSchema中

【问题讨论】:

    标签: node.js mongodb mongoose schema endpoint


    【解决方案1】:

    对于 blogPosts 模型,您需要将 cmets 数组存储为对评论的引用,使用类型对象 id 和 ref:"comment"

    comment: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: "commentSchema",
    }]
    

    现在当评论添加到帖子时,将评论文档ID添加到blogpost->comment数组。 要获取博文中的 cmets,请使用聚合或填充与博文相关的 cmets。

    【讨论】:

    • 如何将评论文档id添加到blogPost.comment数组中?
    • 有两种方法可以做到这一点。 1) 创建评论并将其保存在数据库中,现在使用该文档的 id 并将其推送到 blogPost.comment。 2) 使用 objectID 函数创建自定义 objectID 并将 id 保存在 blogpost.comment 中,并创建具有相同 id 的评论。更推荐第一种方法
    • 我首先创建评论,保存它,然后使用 findOneAndUpdate 将其存储在博客中:
    • const newComment = new Comment({ comment, userId: req.user._id });等待 newComment.save();常量更新 = { $push: { 评论:newComment, }, }; const post= await blogPost.findOneAndUpdate( { _id: req.params.post }, update );
    【解决方案2】:

    我的做法:

    另一个文件中的分隔注释模型:

      const mongoose = require("mongoose");
        
        const commentSchema = new mongoose.Schema({
          date: {
            type: Date,
            default: Date.now,
          },
          comment: {
            type: String,
            max: 1024,
            min: 5,
          },
          userId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User",
          },
        });
        
        module.exports = mongoose.model("Comment", commentSchema);
    

    按照 Mohit Gupta 在他的回答中建议的那样更改评论字段:

    const mongoose = require("mongoose");
    
    const blogSchema = new mongoose.Schema({
      //OTHER FIELDS ARE HERE.....
      comment: [
        {
          type: mongoose.Schema.Types.ObjectId,
          ref: "Comment",
        },
      ],
    });
    
    module.exports = mongoose.model("Blog", blogSchema);
    

    然后我做了这样的路线:

    //COMMENT
    router.post("/comment/:post", verify, async (req, res) => {
    
      const { comment } = req.body;
      if (!comment) {
        return res
          .status(422)
          .send({ error: "Must provide comment with at least 5 symbols" });
      }
    
      try {
        const newComment = new Comment({ comment, userId: req.user._id });
        await newComment.save();
        const update = {
          $push: {
            comment: newComment,
          },
        };
        const post= await Blog.findOneAndUpdate(
          { _id: req.params.post },
          update
        );
    
        res.send(post);
      } catch (err) {
        res.status(422).send({ error: err.message });
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多