如何一次替换多个 mongodb 文档中嵌套数组中的值

How to replace the value in nested arrays in multiple mongodb docs at once

在这里,我想将 country 的值替换为其他任何出现的值,例如“India”。 我有多个结构相同的文档,想一次全部更新。它应该不会影响其他密钥,只是想更新国家/地区。

尝试使用 Set 运算符但未正确获取。

{
    "_id" : "1",
    "teams" : 
    [ 
        {
            "type" : "local",
            "isEnabled" : "true",
            "Country":"India"
            "names" : 
            [ 
                { "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
                { "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
            ]
        },
        {
            "type" : "national",
            "isEnabled" : "true",
            "Country":"India"
            "names" : 
            [ 
                { "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
                { "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
            ]
        },
        {
            "type" : "international",
            "isEnabled" : "true",
            "Country":"England"
            "names" : 
            [ 
                { "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
                { "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
            ]
        },
        {
            "type" : "national",
            "isEnabled" : "true",
            "Country":"India"
            "names" : 
            [ 
                { "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
                { "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
            ]
        },
        {
            "type" : "international",
            "isEnabled" : "true",
            "Country":"Newzealand"
            "names" : 
            [ 
                { "name": "kumar","Nationality":"indian","BirthPlace":"Goa","Age":"U25" },
                { "name": "kannan","Nationality":"indian","BirthPlace":"Kerala","Age":"U25"}
            ]
        }
    ]
}

试试这个:

db.collection.updateMany(
   {},
   { $set: { "teams.$[element].Country": "Republic of India" } },
   { arrayFilters: [{ "element.Country": "India" }] }
);

试试这个:

db.collection.updateMany({},
 {
   $set: {
    "teams.$[].Country": "India"
   }
})