简体   繁体   中英

How can I retrieve information from one collection into another collection in mongoose?

I have a student collection when I want to report on a particular student with
studentId the report must be stored in report collection along with studentId and what report I gave.I want to use individual collections
here is student schema

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

// create a 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
    });

and here is 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;

So how can I retrive studenId from student collection ?And how can I give report to a particular student?

    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 I have been using express.js for your case

    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");

            });  

        });

your answer here

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
  });
});

here is mongoose populate official docs still if you have some other basic thing about mongoose or mean stack means kinldy refer to https://github.com/Muthukumars1994/Meanapp

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