【问题标题】:Updating nested array not working - MongoDB更新嵌套数组不起作用 - MongoDB
【发布时间】:2016-06-09 09:17:18
【问题描述】:

我正在使用这个查询:

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

路由调用:

localhost:3131/api/v0.1/charting/questions/56cff03e9ff240192da2fa34/set/56cff04e9ff240192da2fa3a/add-new-question

在哪里:charting/questions/:questions_id/set/:set_id/add-new-question

文档数据:

/* 1 */
{
    "_id" : ObjectId("56cff03e9ff240192da2fa34"),
    "questionSets" : [ 
        {
            "name" : "Physical exam questions",
            "_id" : ObjectId("56cff03e9ff240192da2fa35"),
            "questions" : [ 
                {
                    "question" : "What is love?",
                    "answer" : "",
                    "_id" : ObjectId("56cff03e9ff240192da2fa39")
                }
            ]
        }, 

        {
            "name" : "Brain questions",
            "_id" : ObjectId("56cff04e9ff240192da2fa3a"),
            "questions" : [ 
                {
                    "question" : "What is love?",
                    "answer" : "",
                    "_id" : ObjectId("56cff04e9ff240192da2fa3e")
                }
            ]
        }
    ],
    "updatedAt" : ISODate("2016-02-26T06:26:39.330Z")
}

我想把这个 JSON 对象推送到questionSets[0].questions

[

    {
        "question" : "Added 1?"
    }
]

但是查询返回 { ok: 0, n: 0, nModified: 0 } 并且文档没有被更新。我在这里做错了什么?谢谢。

【问题讨论】:

    标签: mongodb mongoose mongoose-schema


    【解决方案1】:

    有数据

    > db.questions.find().forEach(printjson)
    {
            "_id" : ObjectId("56d0143bd43e3500f6768488"),
            "questionSets" : [
                    {
                            "name" : "q1",
                            "_id" : "1",
                            "question" : [
                                    {
                                            "question" : "here",
                                            "answer" : "dd",
                                            "_id" : "1"
                                    }
                            ]
                    },
                    {
                            "name" : "q2",
                            "_id" : "2",
                            "question" : [
                                    {
                                            "question" : "you",
                                            "answer" : "cc",
                                            "_id" : "2"
                                    }
                            ]
                    }
            ]
    }
    

    运行命令

    > db.questions.update({'questionSets._id': '1'}, 
                          {$pushAll: {
                                    'questionSets.$.question': [
                                                        {question: 'you', 
                                                         answer: 'are',
                                                         _id: '3'}]}})
    
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    

    【讨论】:

    • 嗨。它返回了这个错误。 { name: 'MongoError', message: 'Cannot apply the positional operator without a corresponding query field containing an array.', driver: true, ok: 1, n: 0, code: 16650, errmsg: 'Cannot apply the positional operator without a corresponding query field containing an array.', writeConcernError: { code: 16650, errmsg: 'Cannot apply the positional operator without a corresponding query field containing an array.' } }
    • @TheGreenFoxx,抱歉之前的粗心,req.body的值是数组吗?
    • 是的,它看起来像这样。 [ {"question" : "Added 1?"} ]
    • @TheGreenFoxx,我已经用我的测试代码更新了我的答案。请看一下,希望对您有所帮助...
    • @TheGreenFoxx,这个"doc.questionSets.$._id": req.params.set_id 在你的测试中效果好吗?
    猜你喜欢
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    相关资源
    最近更新 更多