2016-02-26 40 views
1

我有他的模型:使用mongoose更新mongo中的二級數組返回意外標記「。」

var field = { 
    questionSets: [ 
     { 
      name : "", 
      questions: [ 
       { 
        question: {type: String, required: true}, 
        answer: {type: String}, 
       } 
      ] 
     } 
    ] 
} 

而這個查詢:

SubjectiveForm.update(
    {_id:doc._id, questionSets.$._id:req.params.set_id}, 
    {$pushAll: {questions:req.body}}, 
    {upsert:true}, 
    function(err, questions){ 
     console.log("err", err); 
     console.log("err", questions); 
    } 
) 

,但此行{_id:doc._id, questionSets.$._id:req.params.set_id},questionSets.$返回Unexpected token .

BTW的req.body看起來是這樣的(一個JSON):

[ 
    { 
     "question" : "Added 1?" 
    }, 
    { 
     "question" : "Added 2?" 
    } 
] 

回答

2

由於questionSets.$._id是JSON對象的關鍵,你提供的更新查詢,它應該是'questionSets.$._id'(帶引號),它不能有鑰匙包括點在內

SubjectiveForm.update(
    {_id:doc._id, 'questionSets.$._id':req.params.set_id}, 
    {$pushAll: {questions:req.body}}, 
    {upsert:true}, 
    function(err, questions){ 
     console.log("err", err); 
     console.log("err", questions); 
    } 
) 
+0

WTF?我很盲目。 >。<感謝此。無論如何,上面的查詢在執行時返回'{ok:0,n:0,nModified:0}'它仍然不會更新文檔。 – CENT1PEDE

+0

是的,所以你的查詢現在不匹配任何文件 –

+0

但是數據存在:/ – CENT1PEDE