2016-10-24 117 views
0

數組我有亮點的模式爲:如何更新貓鼬

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   
      }] 
}); 

以下數據是數據庫已經存在:

{ 
     "_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 
} 

我想更新object id =580daa749c6e2e1830a54fe6title and description以上數據。

我做了模型像這樣的靜態函數:

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); 
    }) 
} 

以上funtion的SAVEDATA是:

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

這不是在貓鼬工作。任何想法? **請注意,我想用貓鼬做這件事。不在MongoDB客戶端。 **

回答

0

使用$,

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

CF:Update nested mongo array

+0

早些時候,我想,也把 「$」 沒有幫助。它仍然沒有更新。 – Neo

+0

可能是_id字段的問題,請嘗試使用ObjectId:const ObjectId = mongoose.Types.ObjectId; {'highlight._id':new ObjectId(savedata._id)}, – Dafuck