0

I have a schema of highlight as:

var newshighlightSchema = new mongoose.Schema({
    volumeNo:String,
    week:String,
    status:String,
    adminApp : Boolean,
    reviewerApp:Boolean,
    ttheadApp:Boolean,
    comment:String,

    highlight:[{
            createdby:String,
            level : String,
            title : String,
            description: String        
            }]
});

the following data is already present in db:

{
        "_id" : ObjectId("580daa749c6e2e1830a54fe5"),
        "volumeNo" : "100",
        "week" : "W44Y2016",
        "status" : "false",
        "adminApp" : false,
        "reviewerApp" : false,
        "ttheadApp" : false,
        "comment" : "",
        "highlight" : [
                {
                        "createdby" : "58047db9f8995a147ce3fmeo",
                        "level" : "Accolades",
                        "title" : " the news title",
                        "description" : "Description of news title",
                        "_id" : ObjectId("580daa749c6e2e1830a54fe6")
                }
        ],
        "__v" : 0
}

I want to update the title and description of object id =580daa749c6e2e1830a54fe6 above data.

I made a static function in model like this:

newshighlightSchema.statics.updateLevel=function(savedata, callback){ 
        console.log("data is model updating ",savedata);

    this.update({'highlight._id':savedata._id},{'$set':{
        'highlight.title':savedata.title,
        'highlight.description':savedata.description
    }},function(err){
        callback(err,null);
        console.log("Err in updating",err);
    })
}

the savedata in above funtion is:

savedata = { createdby: '58047db9f8995a147ce3fmeo',
  timestamp: 'Monday, 24th October 2016',
  testlabel: 'qq',
  level: 'Accolades',
  title: 'Updated news title',
  description: 'updated Description',
  _id: '580daa749c6e2e1830a54fe6',
   }

This is not working in mongoose. Any idea? **Please note that I want to do this in mongoose. Not in mongodb client. **

Neo
  • 366
  • 5
  • 15

1 Answers1

0

Use $,

 this.update({'highlight._id':savedata._id},{'$set':{
       'highlight.$.title':savedata.title,
        'highlight.$.description':savedata.description
  }},function(err){

cf : Update nested mongo array

Community
  • 1
  • 1
Daphoque
  • 4,421
  • 1
  • 20
  • 31
  • Earlier I tried that also putting "$" doesn't help. It is still not updating. – Neo Oct 24 '16 at 09:41
  • maybe a problem with the _id fields, try to use ObjectId : const ObjectId = mongoose.Types.ObjectId; {'highlight._id': new ObjectId(savedata._id)}, – Daphoque Oct 24 '16 at 12:18