簡體   English   中英

MongoDB 參考未使用 / MERN 堆棧創建

[英]MongoDB reference not creating using / MERN Stack

我正在做我最后一年的項目,新的 MERN 堆棧面臨在 mongoDB 文檔中創建參考的問題。 使用兩種型號和一種 controller 將數據保存在不同的文檔中,同時如何將 student_id 保存為 parentdata 文檔中的參考。

准入控制器.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'}

只要確保您是從學生 model 導出學生還是學生

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

您還可以共享父模型和學生模型的完整代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM