Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB-使用不同的值更新数组中对象中的值_Javascript_Arrays_Mongodb - Fatal编程技术网

Javascript MongoDB-使用不同的值更新数组中对象中的值

Javascript MongoDB-使用不同的值更新数组中对象中的值,javascript,arrays,mongodb,Javascript,Arrays,Mongodb,我在trainer_copy collection(MongoDB)中有很多文档,但这里我只展示了2个。我需要更新很多文件 //1 { "jobRoles": [{ "sector": { "id": "11", "name": "Electronics & Hardware&q

我在trainer_copy collection(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中有任何方法或任何不同的查询,这将对我非常有帮助。对不起,我不能给这个问题起个更好的名字

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[“名称”]是旧名称,并将其更新为新名称,即x[“新名称”]

在上面的回答中,名称jobRole是数组中与筛选器{“jobRole.sector.name”:x[“name”]}匹配的索引的占位符


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

我不确定这是否是答案,但我注意到在您的查询中,您使用了
jobRoles.sector.name
,但在更新中,您使用了
jobRoles.$.sector.name
$/code>。如果这是正确的,我会给出一个答案。对于匹配,我使用jobRoles.sector.name,但是对于数组中的更新,如果不使用$,它将抛出一个错误。我想您确实想要
$[]
$
单独表示只更新第一项,
$[]
表示所有匹配项,而不是所有匹配项(难以描述)。请看这里,我尝试了“jobRoles.$[].sector.name”,但它被更新为所有用于构造的名称,也被更新为“Electronics and Hardware”,这里它们只显示在一个数组中,而不显示在一个对象中,该数组中的对象具有不同的值。天哪,我希望您没有在生产中运行它!如果没有,您可以尝试其他方法,我认为您希望使用选项
arrayFilters
来匹配,而不是使用查询参数。请参阅该链接中的最后一个示例(很抱歉,我没有仔细查看),我不确定这是否是答案,但我注意到在您的查询中,您使用了
jobRoles.sector.name
,但在更新中,您使用了
jobRoles.$.sector.name
$/code>的名称。如果这是正确的,我会给出一个答案。对于匹配,我使用jobRoles.sector.name,但是对于数组中的更新,如果不使用$,它将抛出一个错误。我想您确实想要
$[]
$
单独表示只更新第一项,
$[]
表示所有匹配项,而不是所有匹配项(难以描述)。请看这里,我尝试了“jobRoles.$[].sector.name”,但它被更新为所有用于构造的名称,也被更新为“Electronics and Hardware”,这里它们只显示在一个数组中,而不显示在一个对象中,该数组中的对象具有不同的值。天哪,我希望您没有在生产中运行它!如果没有,您可以尝试其他方法,我认为您希望使用选项
arrayFilters
来匹配,而不是使用查询参数。请参阅该链接中的最后一个示例(很抱歉,我没有仔细查看)