2
我想更新數組中的子字典。如何用Mongodb中的子字典更新數組
我的數組這樣
"comments" : [
{
"text" : "hi",
"_id" : ObjectId("56c552dd0a0f08b502a56521"),
"author" : {
"id" : ObjectId("56c54c73f96c51370294f254"),
"photo" : "",
"name" : ""
}
},
{
"text" : "Good",
"_id" : ObjectId("56c5911fc33b446c05a4dabc"),
"author" : {
"id" : ObjectId("56c54be6f96c51370294f123"),
"name" : "xxxx",
"photo":null
}
}
]
我要爲這個ID author.id:"56c54be6f96c51370294f123"更新的名字和照片。
我寫了這樣的代碼:
Event.find({'comments.author.id':_id},(err,event)=>{
_.each(event,function(eventData){
_.each(eventData.comments,function(commentData){
if(commentData.author.id == _id){
var data={'name':username,'photo':photoUrl};
Event.update({'commentData.author.id':_id},
{"$set":{"eventData.comments.author":data}},(err,commentupdate)=>{
console.log("commentupdate:"+JSON.stringify(commentupdate))
})
}
})
})
})
但我無法更新數據,請給我任何建議。謝謝
@ chridam:感謝您的回覆,我已經嘗試過,但得到的控制檯響應like commentupdate:{「ok」:1,「n」:0,「nModified」:0}。 – Mahesh
你能說明你的'Event'模型模式是如何精確定義的嗎? – chridam
@ chridam:請參考這個鏈接其實我想要這個http://stackoverflow.com/questions/36917072/how-to-update-multiple-records-in-same-array-in-a-single-collection-in -mongodb – Mahesh