繁体   English   中英

如何在MongoDB中的嵌套数组中更新“键-值”对的值而不添加新元素

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

我有以下数据:-** {

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

        ]
    }
]

} **现在,我想将“ OS_VERSION”的值更新为“ 12”,而不是“ 10”。 更新后,它应如下所示:-** {

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

        ]
    }
]

} **

当我将更新运算符与“ $ addToSet”一起使用时,将添加新的键值对,而不是像这样更新现有数据:** {

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

} **我不希望将其添加为新元素,而是应该仅更新现有元素。 我怎样才能达到预期的效果。 有人可以帮忙吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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