简体   繁体   中英

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. 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.

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()

in parentModel

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

Just Make sure if you are exporting Student or student from student model

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

Also can you share the complete code of both parent and student models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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