【问题标题】:MongoDB - Update values in object inside an array with different valuesMongoDB - 使用不同值更新数组内对象中的值
【发布时间】:2020-07-29 05:31:57
【问题描述】:

我在 trainer_copy 集合 (MongoDB) 中有很多文档,但这里我只显示了 2 个。我需要更新很多文档。

//1

  {
    "jobRoles": [{
            "sector": {
                "id": "11",
                "name": "Electronics & Hardware"
            }
        },
        {
            "sector": {
                "id": "23",
                "name": "Management"
            }
        },
        {
            "sector": {
                "id": "9",
                "name": "Construction"
            }
        },
        {
            "sector": {
                "id": "11",
                "name": "Electronics & Hardware"
            }
        },
    ]
}

//2
{
    "jobRoles": [{
            "sector": {
                "id": "20",
                "name": "Iron & Steel"
            }
        },
        {
            "sector": {
                "id": "20",
                "name": "Iron & Steel"
            }
        },
        {
            "sector": {
                "id": "9",
                "name": "Construction"
            }
        },
    ]
}
}

我需要更新的结果如下:

//1
{
    "jobRoles" : [ 
        {
            "sector" : {
                "id" : "11",
                "name" : "Electronics and Hardware"
            }
        }, 
        {
            "sector" : {
                "id" : "23",
                "name" : "Management"
            }
        }, 
        {
            "sector" : {
                "id" : "9",
                "name" : "Construction"
            }
        }, 
        {
            "sector" : {
                "id" : "11",
                "name" : "Electronics and Hardware"
            }
        }, 
]
}

//2
{
    "jobRoles" : [ 
        {
            "sector" : {
                "id" : "20",
                "name" : "Iron and Steel"
            }
        }, 
        {
            "sector" : {
                "id" : "20",
                "name" : "Iron and Steel"
            }
        }, 
        {
            "sector" : {
                "id" : "9",
                "name" : "Construction"
            }
        }, 
    ]
}
}

我试过这个方法

var tc = [
    {"name":"Electronics & Hardware",            "new_name" :  "Electronics and Hardware"},
    {"name":"Furniture & Fittings",              "new_name" :  "Furniture and Fittings"},
    {"name":"Gems & Jewellery",                  "new_name" :  "Gems and Jewellery"},
    {"name":"Instrumentation",                   "new_name" :  "IASC"},
    {"name":"Iron & Steel",                      "new_name" :  "Iron and Steel"},
    ]
    
    
tc.forEach(x => {

db.trainer_copy.updateMany({"jobRoles.sector.name":x["name"]},
{
    $set: {
        "jobRoles.$.sector.name": x["new_name"]
        }
    })
})

但我得到的结果如下

//1
{
    "jobRoles" : [ 
        {
            "sector" : {
                "id" : "11",
                "name" : "Electronics and Hardware"
            }
        }, 
        {
            "sector" : {
                "id" : "23",
                "name" : "Management"
            }
        }, 
        {
            "sector" : {
                "id" : "9",
                "name" : "Construction"
            }
        }, 
        {
            "sector" : {
                "id" : "11",
                "name" : "Electronics & Hardware"
            }
        }, 
]
}

//2
{
    "jobRoles" : [ 
        {
            "sector" : {
                "id" : "20",
                "name" : "Iron and Steel"
            }
        }, 
        {
            "sector" : {
                "id" : "20",
                "name" : "Iron & Steel"
            }
        }, 
        {
            "sector" : {
                "id" : "9",
                "name" : "Construction"
            }
        }, 
    ]
}
}

只有 jobRoles 中的第一个对象在更新,其余的保持不变。据我所知,我不能使用$[] 位置参数,因为它会更新数组内的所有对象。我不想那样。

我知道另一种方法,例如每次我必须使用 if 条件来更新其值时。但是对于 25 个或更多条件,如果条件每次都很难编写。如果在 javascript 或 MongoDB 中有任何方法或任何不同的查询,那将对我非常有帮助。抱歉,我无法为这个问题提供更好的名称。

【问题讨论】:

  • 我不确定这是不是答案,但我注意到在您的查询中您使用了jobRoles.sector.name,但在您的更新中您使用了jobRoles.$.sector.name$。如果这是正确的,我会回答它。
  • 为了匹配我正在使用 jobRoles.sector.name 但如果你不使用 $ 来更新数组内部,它会抛出一个错误
  • 我明白了。我想你确实想要$[]$ 单独表示仅更新第一项,$[] 表示所有匹配项,而不是全部(不加区分地)。见这里docs.mongodb.com/manual/reference/operator/update/…
  • 我试过“jobRoles.$[].sector.name”,但它更新了所有的建筑名称,也更新为“电子和硬件”,这里docs.mongodb.com/manual/reference/operator/update/…他们只显示在里面一个不在对象内的数组,该数组在该数组中具有不同的值
  • 天哪,我希望你没有在生产环境中运行它!如果没有,您可以尝试其他方法,我认为您想使用选项arrayFilters 来匹配而不是查询参数。请参阅该链接中的最后一个示例(对不起,我没有仔细查看)

标签: javascript arrays mongodb


【解决方案1】:
var tc = [
    {"name":"Electronics & Hardware",            "new_name" :  "Electronics and Hardware"},
    {"name":"Furniture & Fittings",              "new_name" :  "Furniture and Fittings"},
    {"name":"Gems & Jewellery",                  "new_name" :  "Gems and Jewellery"},
    {"name":"Instrumentation",                   "new_name" :  "IASC"},
    {"name":"Iron & Steel",                      "new_name" :  "Iron and Steel"},
    ]
    
    
tc.forEach(x => {

db.trainer_copy.updateMany({"jobRoles.sector.name":x["name"]},
{
    $set: {
        "jobRoles.$[jobRole].sector.name": x["new_name"]
        }
    },
    { arrayFilters: [{ "jobRole.sector.name": x["name"] }] }
    )
})

在上面的答案中,名称 jobRole 是数组中与过滤器匹配的索引的占位符 { "jobRole.sector.name" : x ["name"] }。

x ["name"] 是旧名称并将其更新为新名称,即 x["new_name"]。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2023-02-11
    • 2021-12-06
    相关资源
    最近更新 更多