使用mongoose更新mongo中的二级数组返回Unexpected token“。”
我有他的模型:
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},
返回Unexpected token .
在questionSets.$
。
BTW req.body
看起来像这样(一个JSON):
[ { "question" : "Added 1?" }, { "question" : "Added 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); } )