更新mongodb中两个对象内的数组

时间:2015-04-07 10:20:11

标签: javascript mongodb

我有如下结构的集合:

集合名称:“职位”

结构

{
    "_id" : "vtQ3tFXg8THF3TNBc",
    "candidatesActions" : {
        "sourced" : [ ],
    },

    "appFormObject" : {
        "name" : "✶ mandatory",
        "questions" : [
            {
                "qusId" : "qs-585494",
                "type" : "simple",
                "qus" : "Which was your previous company"
            },
            {
                "qusId" : "qs-867766",
                "type" : "yesNo",
                "qus" : "Are you willing to relocate?",
                "disqualify" : "true"
            }
        ]
    }
}

我想更新上面集合的“qus”字段,其中_id是“vtQ3tFXg8THF3TNBc”而“qusId”是“qs-585494”。

2 个答案:

答案 0 :(得分:1)

尝试以下....

db.positions.update(
  {_id: "vtQ3tFXg8THF3TNBc", "appFormObject.questions.qusId":"qs-585494"},
  {$set:{"appFormObject.questions.$.qus": "this is updated value"}}
)

答案 1 :(得分:0)

使用以下查询

db.positions.findAndModify({
    query: { _id: "vtQ3tFXg8THF3TNBc", "appFormObject.questions.qusId":"qs-585494"} ,
    update: { $set: { 'appFormObject.questions.$.qus': 'Brilliant Green' } },
});

由于