【问题标题】:MongoDB reference not creating using / MERN StackMongoDB 参考未使用 / MERN Stack 创建
【发布时间】:2021-07-18 02:23:25
【问题描述】:

我正在做我最后一年的项目,新的 MERN 堆栈面临在 mongoDB 文档中创建引用的问题。 使用两个模型和一个控制器将数据保存在不同的文档中,同时如何将 student_id 保存为 parentdata 文档中的引用。

AdmissionController.js

  const newStudent = new studentModel({
          s_fname,
          s_lname,
          gender,
          age
     })
     const ParentData = new parentModel({
           fatherName,
           father_cnic, 
           FatherDesignation, 
           fatherMobile,
           motherName    
     })
     newStudent.save()
     ParentData.save()

在父模型中

 student_id: { type: Schema.Types.ObjectId, ref:'student'}

【问题讨论】:

    标签: node.js mongodb express


    【解决方案1】:

    只需确保您是从学生模型中导出 Student 还是 student

    student_id: {
          type: Schema.Types.ObjectId,
          ref: "Student",
        },
    

    你也可以分享一下父模型和学生模型的完整代码。

    【讨论】:

      猜你喜欢
      • 2023-01-26
      • 1970-01-01
      • 2016-03-19
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2022-11-17
      • 2021-03-30
      相关资源
      最近更新 更多