简体   繁体   中英

Mongoose schema for array of objects and upon update objects should get added

var mongoose = require('mongoose');
module.exports = mongoose.model('GridModel',  {  
    Request_Id : { type : Number, required : true },    
    viewStudents : { type : Array , default : [] }
});

The above one is the mongoose model and after update to the viewStudents fileds should get added.

Query for update is :

var conditions = { Request_Id : req.body.Request_Id},
    update = {  
       viewStudents : {   
         Student_Name:req.body.Student_Name,
         Student_Id:req.body.Student_Id,
         Resume:req.body.Resume}
    },
    options = { multi : true};

GridModel.update(conditions, update, options, callback);

function callback(err,res2) {
    if(err)
        res.send(err);
    getGridRequests(res);
}

Final output after two updates should look like

viewStudents: {
    {
         Student_Name: asa,
         Student_Id : 3,
         Resume : No
    } 
    {
        Student_Name: asfsdfa,
        Student_Id : 34234,
        Resume : No
    }
}

But I am not getting the way I have shown with the above code

GridModel has no Request_Id property. Update should have $push modificator:

{  
    viewStudents : {
        $push: {
            Student_Name:req.body.Student_Name,
            Student_Id:req.body.Student_Id,
            Resume:req.body.Resume
        }
    }
}

NOTE Upper camel case with underscores looks very strange to me.

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