【问题标题】:Update documents nested multiple levels in an array更新嵌套在数组中的多个级别的文档
【发布时间】:2019-09-05 16:43:30
【问题描述】:

我在 MongoDb 中有这个 JSON 结构,并且想要更新更改嵌套数组中特定项目的特定值。我想将键 targetAreaIdUSER_MESSAGE 更改为 VISUAL_MESSAGE

{
"_id" : "5cde9f482f2d5b924f492da2",
"scenario" : "SCENARIOX",
"mediaType" : "VISUAL",
"opCon" : "NORMAL",
"stepConfigs" : [ 
    {
        "stepType" : "STEPX",
        "enabled" : true,
        "configs" : [ 
            {
                "contentTemplateId" : "5cde9f472f2d5b924f492973",
                "scope" : "STANDARD",
                "key" : "CONTENT"
            }, 
            {
                "priorityId" : "5cde9f472f2d5b924f49224f",
                "scope" : "STANDARD",
                "key" : "PRIORITY"
            }, 
            {
                "targetAreaId" : "USER_MESSAGE",
                "scope" : "STANDARD",
                "key" : "TARGET_AREA"
            }
        ],
        "description" : "XPTO"
    }
],
"scope" : "STANDARD" }

我怎样才能同时做到呢?

编辑

我正在尝试这种方式:

var cursor = db.getCollection('CollectionX').find({
  "scenario": "SCENARIOX",
  "stepConfigs.stepType": "STEPX",
  "stepConfigs.configs.key": "TARGET_AREA"
});

if (cursor.hasNext()) {
    var doc = cursor.next();
    doc.stepConfigs.find(function(v,i) { 
        if (v.stepType == "STEPX") {
           doc.stepConfigs[i].configs.find(function(w,j) {
                if (w.key == "TARGET_AREA") {
                    var result = db.getCollection('CollectionX').update(
                        { "_id" : doc._id },
                        { "$set" : { doc.stepConfigs[i].configs[j].targetAreaId: "VISUAL_MESSAGE" }}
                    );
                }
           });
        };
    });
} else {
    print("Step does not exist");
}

但出现以下错误:

错误:第 15 行:意外令牌。

【问题讨论】:

    标签: arrays mongodb mongodb-query


    【解决方案1】:

    我认为这是不可能的。

    您可以使用此查询更新您想要的特定元素:

    db.test_array.updateOne({}, {
        $set: {
            "stepConfigs.$[firstArray].configs.$[secondArray].targetAreaId": "VISUAL_MESSAGE"
        }
    }, {
        arrayFilters: [{
            "firstArray.stepType": "STEPX"
        }, {
            "secondArray.targetAreaId": "USER_MESSAGE"
        }]
    })
    

    但是同时推入同一个数组确实会呈现这个(这是针对原始模型的,但仍然是同一个问题,您不能以这种方式将 $set 和 $push 放在同一个数组中):

    > db.test_array.updateOne({"step.type":"B"},{$push:{"step.$.configs":{"className":"something"}}, $set:{"step.$.configs.$[secondArray].targetAreaId":"TA-1"}},{arrayFilters:[{"secondArray.targetAreaId":"TA-2"}]})
    2019-09-06T13:53:44.783+0200 E QUERY    [js] WriteError: Updating the path 'step.$.configs.$[secondArray].targetAreaId' would create a conflict at 'step.$.configs' :
    

    【讨论】:

    • 出现这个错误:"errmsg" : "不能使用部分(step of step.$[firstArray].configs.$[secondArray].targetAreaId)遍历元素
    • 你能给我完整的文件吗?我根据自己的文档是这个:{ "_id" : "1", "className" : "com.BLABLA", "scenario" : "A", "step" : [ { "type" : "B", "enabled" : true, "configs" : [ { "className" : "com.XPTO", "targetAreaId" : "TA-1", "scope" : "S", "key" : "TA" }, { "className" : "something" } ], "description" : "XXXX" } ] }
    • 更新了答案。它仍然可以更新特定元素。但不要推。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2015-07-13
    • 2017-04-28
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多