简体   繁体   中英

How to update the values of a “Key-Value” pair in a nested array in MongoDB without adding a new element

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.

首先删除该键值对,然后插入新的键值对。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM