node.js - 使用 mongoose 更新 mongodb 中的第二级数组返回意外标记 "."

标签 node.js mongodb express mongoose

我有他的模型:

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/

相关文章:

c# - 如何将 C# 中的嵌套集合插入到 mongo db

javascript - 动态导航到 Express 服务器的 React App 链接

node.js - tls.connect 何时调用 'end' 或 'close'

javascript - typescript 对象模式?

mongodb - 选择按字段分组的文档

mongodb - 警告错误 : Meteor code must always run within a Fiber when call method on server

javascript - Express 中的 EJS 变量引用显示为未定义

node.js - 为什么我的 Node 应用程序在每次刷新后都会继续发送数据?

javascript - 如何安装语义用户界面?

javascript - 我在 Node.js 中读取 tgz 文件的方式有问题吗?基准测试显示速度很慢 :(