0

I have the following data:- **{

"_id" : ObjectId("5bf8048768d1e82d8bb4a477"),
"customer_code" : "c100003",

"vm_info" : [ 
    {

        "instanceId" : "i-0495d75742b839858",
        "tags" : [ 

            {
                "Key" : "SIDs",
                "Value" : "NWS"
            },  
            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            },  
            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            }, 
            {
                "Key" : "OS_VERSION",
                "Value" : "10"
            }
        ]
    },

    {
        "instanceId" : "i-017488cffca28cd70",
        "tags" : [ 

            {
                "Key" : "OS_VERSION",
                "Value" : "10"
            }, 

            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            }, 


            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            }, 
            {
                "Key" : "SIDs",
                "Value" : "NWS"
            }
       ]

    }, 

    {
        "instanceId" : "i-09d5db81657fe35d8",
        "tags" : [ 
            {
                "Key" : "SIDs",
                "Value" : "NWS"
            }, 
            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            }, 
            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            }, 
            {
                "Key" : "OS_VERSION",
                "Value" : "10"
            }

        ]
    }
]

}** Now i want to update the value of "OS_VERSION" to "12" instead of "10". After updating it should look like this:- **{

"_id" : ObjectId("5bf8048768d1e82d8bb4a477"),
"customer_code" : "c100003",
"vm_info" : [ 
    {
        "instanceId" : "i-0495d75742b839858",
        "tags" : [ 

            {
                "Key" : "SIDs",
                "Value" : "NWS"
            }, 
            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            }, 
            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            },                               
            {
                "Key" : "OS_VERSION",
                "Value" : "12"
            }
        ]
    },

    {
        "instanceId" : "i-017488cffca28cd70",
        "tags" : [ 

            {
                "Key" : "OS_VERSION",
                "Value" : "10"
            }, 

            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            }, 


            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            }, 
            {
                "Key" : "SIDs",
                "Value" : "NWS"
            }
       ]

    }, 

    {
        "instanceId" : "i-09d5db81657fe35d8",
        "tags" : [ 
            {
                "Key" : "SIDs",
                "Value" : "NWS"
            }, 
            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            }, 
            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            }, 
            {
                "Key" : "OS_VERSION",
                "Value" : "10"
            }

        ]
    }
]

}**

When i use Update operator with "$addToSet" a new Key-Value pair is added instead of updating the existing data like this: **{

"_id" : ObjectId("5bf8048768d1e82d8bb4a477"),
"customer_code" : "c100003",
"vm_info" : [ 
    {
        "instanceId" : "i-0495d75742b839858",
        "tags" : [ 

            {
                "Key" : "SIDs",
                "Value" : "NWS"
            }, 
            {
                "Key" : "OSTYPE",
                "Value" : "WINDOWS"
            }, 
            {
                "Key" : "BACKUP_SERVER_LOCATION",
                "Value" : ""
            }, 
            {
                "Key" : "OS_VERSION",
                "Value" : "10"
            },                              
            {
                "Key" : "OS_VERSION",
                "Value" : "12"
            }
        ]
    }
]

}** I don't want it to be added as a new element ,instead it should just update the existing element. How can i achieve the desired result. Can someone help on this.

  • Possible duplicate of [MongoDB - Update an object in nested Array](https://stackoverflow.com/questions/34431435/mongodb-update-an-object-in-nested-array) – IftekharDani Nov 27 '18 at 05:38
  • No bro a little different.The idea may work if i have only one instance id and tags.But I have 3 Instance IDs similar to above one and each Instance id has a tags. How can i update the Value alone in the Key-Value pair of my data. – Shanmuga Perumal Nov 27 '18 at 06:11

1 Answers1

0

first remove that key-value pair and and insert new one.