2016-02-11 55 views
0

我的MongoDB的結構是這樣的......

Database (Job Application Database) 
    ... 
    Nested Array of Objects (Applicants) 
     ... 
     Another Nested Array of Objects (Comments) 

我有這樣的代碼工作正常更新嵌套數組

JobApplication.findOneAndUpdate(
    { _id: new ObjectId(data.job_app_id), 'applicants._id' : new ObjectId(data.selected_applicant_id) }, 
    {'$push': {'applicants.$.comments': data.message}}, 
    {safe: true, upsert: true, new : true}, 
    function(err, model) { 
     console.log(err); 
     console.log(model); 
    } 
) 

我遇到的問題是它不會返回添加的對象。我不只是返回data.message是因爲我需要對象的_id。有沒有辦法返回添加的對象?

+0

這裏的問題是「upsert」。想一想爲什麼當你在數組中尋找內容時這可能是一個問題。這兩者混合不佳的原因很合理。 –

+0

@BlakesSeven請謹慎解釋原因? – Nicky

回答

0
`JobApplication.findOneAndUpdate(
    { _id: new ObjectId(data.job_app_id), 'applicants._id' : new ObjectId(data.selected_applicant_id) }, 
    {'$push': {'applicants.$.comments': data.message}}, 
    {safe: true, upsert: true, new : true}, 
    function(err, model) { 
if(err) return JSON.stringify(err) 
     if(model){ 
console.log(model) } 
)` 
+0

你得到了什麼輸出/錯誤? – Jagan