i new node.js coming java experience. have situation trying wrap head around. stack express.js, mongoose, ejs template. here scenario: have schema:
var userschema = mongoose.schema({ name: { type: string, index: true }, password: { type: string, select: false }, email: { type: string }, academic: [{ qualification: string, institute: string, from: string, to: string, about: string }]
});
there list of academics. want update 1 academic object in list. how go this?
router.post('/academic/schools/update', function (req, res) {
});
i pass values ejs template route , getting values in req.body. how in node , mongoose query specific object in route , updates values. have thought maybe adding id academic object able keep track of update.
each academic sub document have _id after save. there 2 ways can it. if pass id of user , id of academic sub-doc id in url or request body, can update this:
user.findbyid(userid).then(user => { let academic = user.academic.id(academicid); academic.qualification = 'something'; return user.save(); });
if pass id of academic sub-doc, can this:
user.findone({'academic._id': academicid}).then(user => { let academic = user.academic.id(academicid); academic.qualification = 'something'; return user.save(); });
note sub document array return mongoose mongoosearray instead of native array data type. can manipulate them using .id .push .pop .remove method http://mongoosejs.com/docs/subdocs.html
Comments
Post a Comment