javascript - Adding new record into nested array -


this question has answer here:

i unable add new doctor information under doctor array name :ivf in mongodb using mongoose

hospitalid: 10001, hospitalname: 'renai medicity', __v: 0, updated_at: 2017-08-18t17:34:53.784z, treatment:  [ { name: 'root canal',      costupperbound: 10000,      costlowerbound: 8000,      departmentid: 10001,      procedureid: 10001,      departmentname: 'dental',      _id: 599725530c126c1efc43dc52,      doctor:[ {             profilepicdir:"smdir1",             doctorname:"dr.vp2",             doctorid:10002,_id:5997255c0c126c1efc43dc57               }] },     ], 

i using below code code adding new doctor information

 hospitalmodel.findoneandupdate({                 "hospitalname": hospitalname, "hospitalcontact.city": hospitalcity, "hospitalcontact.country": hospitalcountry, "treatment": {                     $elemmatch: { "name": procedurename }                 }},                 {                     "$push": {                         "treatment": {                             "doctor": {                                 "doctorid": doctorid,                                 "doctorname": req.body["doctorname"],                                 "profilepicdir": req.body["profilepicdirectory"],                                 "medinovitadoctorrating": parseint(req.body["medinovitadoctorrating"]),                                 "speciality": {                                     "specialityname": req.body["specialityname"]                                 }                             }                         }                     }                 },                 { returnoriginal: false, upsert: true }, function (err, doc) {}) 

instead of adding new doctor record name: 'root canal' doctor array,its creating new object under treatment array.

can 1 me resolve this?

using positional $ operator, following should trick:

hospitalmodel.findoneandupdate({                 "hospitalname": hospitalname, "hospitalcontact.city": hospitalcity, "hospitalcontact.country": hospitalcountry, "treatment": {                     $elemmatch: { "name": procedurename }                 }},                 {                     "$push": {                         "treatment.$.doctor": {                             "doctorid": doctorid,                             "doctorname": req.body["doctorname"],                             "profilepicdir": req.body["profilepicdirectory"],                             "medinovitadoctorrating": parseint(req.body["medinovitadoctorrating"]),                             "speciality": {                                 "specialityname": req.body["specialityname"]                             }                         }                     }                 },                 { returnoriginal: false, upsert: true }, function (err, doc) {}) 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -