简体   繁体   English

MongoDB 参考未使用 / MERN 堆栈创建

[英]MongoDB reference not creating using / MERN Stack

i am working on my final year project, new to MERN stack facing issue in creating reference in mongoDB documents.我正在做我最后一年的项目,新的 MERN 堆栈面临在 mongoDB 文档中创建参考的问题。 using two models and one controller to save data in different documents, at the same time how can i save student_id as a ref in parentdata document.使用两种型号和一种 controller 将数据保存在不同的文档中,同时如何将 student_id 保存为 parentdata 文档中的参考。

AdmissionController.js准入控制器.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()

in parentModel在父模型中

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

Just Make sure if you are exporting Student or student from student model只要确保您是从学生 model 导出学生还是学生

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

Also can you share the complete code of both parent and student models.您还可以共享父模型和学生模型的完整代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM