node.js - Mongoose - pushing object into nested array -
i trying push student object inside 'students' object of team object. below details.
i got solution niel. have noted @ end. team.js
var teamschema = new schema({ // team name. name: string, lead: string, students :type: [{ block : number, status : string, student : { type: schema.objectid, ref: 'student' }] });
student.js
var studentschema = new schema({ name: string, rollno : number, class : number });
expected output
team { "__v": 1, "_id": "5252875356f64d6d28000001", "students": [ { "__v": 1, "_id": "5252875a56f64d6d28000002", block : 1, status : invited, student: { "name": sumeeth "rollno" : 2 "class" : 5 } }, { "__v": 1, "_id": "5252875a56f64d6d28000003", block : 1, status : invited, student: { "name": sabari "rollno" : 3 "class" : 4 } } ], "lead": "ratha", }
current output
team { "__v": 1, "_id": "5252875356f64d6d28000001", "students": [ { "__v": 1, "_id": "5252875a56f64d6d28000002", block : 1, status : invited }, { "__v": 1, "_id": "5252875a56f64d6d28000003", block : 1, status : invited } ], "lead": "ratha", }
this js use document using mongoose in teamapi.js
exports.students= { /* * ### create player */ create: function(req, res) { var _team = req.team; var student = req.body; var _student = new student(student); student.create(student); _team.update({ $push: { students: { student: new student(student), block : ‘1’, status: 'invited' } } }); } }
solution
var _student = new student(student); _student.save(); _team.update({ $push: { students: { student: _student, invite: _invite, status: 'invited' } } }
some 1 please me wrong. how should make use of push, , can student object inside team.
Comments
Post a Comment