2017-03-22 7 views
0

私は
で特定の生徒に報告したいとき、私は学生のコレクションを持っているレポートはstudentId とともにレポートコレクションに格納されなければならないと私はここに、個々のコレクションに
を使用したいgave.I何レポートstudentId student schema1つのコレクションから、マングースの別のコレクションに情報を取り出す方法はありますか?

です
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

//スキーマ

var studentSchema = new Schema({ 
     name: { 
      type: String, 
      required: true 
       }, 
     dateofbirth: { 
      type: Date, 
      required: true 
     }, 
     schoolname: 
    { 
    type:String, 
    required:true 
    }, 
    standard: 
    { 
    type: Number, 
    required:true 
    } 

    }, { 
     timestamps: true 
    }); 

を作成し、ここにreport schema

です
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var reportSchema = new Schema({ 
    date: { 
    type:Date, 
    required:true 
    }, 
    comment: 
    { 
    type:String, 
    required:true 
    } 
    }, 
    { 
    timestamps: true 
}); 

var Report = mongoose.model('report', reportSchema); 
module.exports = Report; 

私はどのようにして学生のコレクションからスタディを回収できますか?特定の生徒にどのように報告することができますか?あなたのケースのために、私はexpress.jsを使用している

+0

を参照してください意味まだ mongoose populate official docs ですデータベース参照/) –

答えて

1
var mongoose = require('mongoose'); 
    var Schema = mongoose.Schema; 
    var studentSchema = new Schema({ 
      name:   {type: String, required: true}, 
      dateofbirth: {type: Date, required: true}, 
      schoolname: {type:String, required:true}, 
      standard:  {type: Number, required:true}, 
      timestamps: true 
     }); 
    var reportSchema = new Schema({ 
     date: {type:Date,required:true}, 
     comment: {type:String,required:true} 
      timestamps: true, 
      student_id: [{type: String, ref:'student',required: true}] //store your student{_id} relation here ref ==> is just a Collection name 
     }); 
mongoose.connect('mongodb://localhost/your_db_name_here', { 
    // using mongoose client to avoid promises exception 
    useMongoClient: true, 
}); 

    //just making your collection available to next controller.js file 
    module.exports= { 
     student : mongoose.model('student',studentSchema), 
     report: mongoose.model('report', reportSchema) 
     }; 

controller.js

var db = require("./db.js"); 
    var app = require("express")(); 
    app.post("/api/student/register", (req, res) => { 
     var dbdata = { 
       name: req.body.name, 
       ....... 
      } 
     db.student.create(dbdata, (er, callback) => { 
      if(er)return res.json("error"); 
         return res.json("successfully inserted"); 

      }); 
     }); 
     app.post("/api/student/report/create", (req, res) => { 
      //while creating a report you have to pass a Student primary key 
      var dbdata = { 
        date: req.body.data; 
        comment: req.body.comment, 
        student_id: req.body.student_id 
     // similar to foreign key relation ship} 
       } 
       db.report.create(dbdata, function(er, callback){ 
      if(er)return res.json("err"); 
      return res.json("successfully inserted"); 

      }); 

     }); 

ここにあなたの答え

app.post("/particular/student/report", function(req, res){ 
    db.report.find({student_id:"your student id here"}).populate('student_id').exec(function(err, data){ 
    if(err)return res.json("db exception"); 
    return res.json(data); //you will get all your student report for the particular student 
    }); 
}); 
ここ

あなたはマングースに関するいくつかの他の基本的なことを持っているか、スタックを意味する場合kinldyはhttps://docs.mongodb.com/manual/reference/([ここ]を見てhttps://github.com/Muthukumars1994/Meanapp

+0

上記のコードはあなたの問題を解決しました – muthukumar

関連する問題