更新双嵌套数组mongodb

时间:2019-07-29 18:18:42

标签: mongodb mongodb-query

我有以下文档,其中包含双嵌套数组格式。当“ someKey”:“ somevalue”和“ Company”:“ Company1”和“ Name”:“ Nandhi”时,我必须将“级别”字段更新为“高级工程师”。

文档

    { 
        "_id" : "777", 
        "someKey" : "someValue", 
        "someArray" : [
            {
                "Company" : "Company1", 
                "someNestedArray" : [
                    {
                        "name" : "Nandhi", 
                        "level" : "Junior Engineer"
                    }, 
                    {
                        "name" : "Rajan", 
                        "level" : "Senio Engineer"
                    }
                ]
            }], 
            {
                "Company" : "Company2", 
                "someNestedArray" : [
                    {
                        "name" : "Nandhi", 
                        "level" : "Junior Engineer"
                    }, 
                    {
                        "name" : "Rajan", 
                        "level" : "Senio Engineer"
                    }
                ]
            }
        ]
    }

更新我尝试过的查询

    db.Test123.updateOne(
    {"someKey" : "someValue","someArray.Company":"Company1"},
    {$set:{"someArray.$[someNestedArray].level":"Senior Developer"}},
    {arrayFilters:[{"someNestedArray.name":"Nandhi"}]}
    );

输出屏幕截图

enter image description here

如您所见,modifiedCount返回0。请对此提供建议!

1 个答案:

答案 0 :(得分:1)

您需要为每个嵌套级别定义arrayFilter,请尝试:

db.Test123.update(
    { "someKey" : "someValue" },
    { "$set": { "someArray.$[someArrayDoc].someNestedArray.$[someNestedArrayDoc].level": "Senior Developer" } },
    { arrayFilters: [ {"someArrayDoc.Company": "Company1"}, { "someNestedArrayDoc.name": "Nandhi" } ] }
)