1

Here i wanted to replace the value of country to something else wherever it presents like "India". I have multiple docs with same structure and would like update all at once. It should not affect other keys and just wanted to update the country.

Tried with Set operator and not getting it correctly.

{
    "_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"}
            ]
        }
    ]
}

2 Answers 2

1

Try this one:

db.collection.updateMany(
   {},
   { $set: { "teams.$[element].Country": "Republic of India" } },
   { arrayFilters: [{ "element.Country": "India" }] }
);
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry was away due to sick. This helps. Thanks a lot
1

Try this one:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.