2017-03-22 61 views
0

我有一個學生集合時,我想在一個特定的學生報告與
studentId報告必須存儲報告收集與studentId ,沿着我gave.I要使用單獨的軟件集合
這裏什麼報告student schema如何從一個集合中將信息從貓鼬中檢索到另一個集合中?

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; 

那麼我怎樣才能從學生收集中收回學生?我怎樣才能給某個學生報告?

+0

看看[這裏](https://docs.mongodb.com/manual/reference/數據庫引用/) –

回答

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我一直在使用express.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 
    }); 
}); 

這裏是 mongoose populate official docs 猶若您對貓鼬其他一些基本的東西或意味着疊層裝置kinldy指https://github.com/Muthukumars1994/Meanapp

+0

上述代碼是否解決了您的問題 – muthukumar

相關問題