我有他的模型:
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 .
。 $
顺便说一句,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);
}
)
关于node.js - 使用 mongoose 更新 mongodb 中的第二级数组返回意外标记 ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35646052/