本文介绍了如何在MongoDB中更新嵌套数组中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的代码中更新嵌套数组中的值,如下所示:-

How to update a values in the nested array my code like below:-

{

    "cats" : [
            {
                    "catId" : "1",
                    "catname" : "elect",
                    "displayname" : "Elect",
                    "subcat" : [
                            {
                                    "subcatname" : "lap",
                                    "subcatId" : "1",
                                    "displayname" : "Laptops"
                            },
                            {
                                    "subcatname" : "cam",
                                    "subcatId" : "2",
                                    "displayname" : "Camera"
                            }
                    ]
            },
            {
                    "catId" : "2",
                    "catname" : "auto",
                    "displayname" : "Auto",
                    "subcat" : [
                            {
                                    "subcatname" : "car",
                                    "subcatId" : "1",
                                    "displayname" : "Car"
                            },
                            {
                                    "subcatname" : "byk", // i want MoterCycle here
                                    "subcatId" : "2",
                                    "displayname" : "Byke"
                            }
                    ]
            }
    ]
 }

我想将 subcatname byk的值更新为MoterCycle 最好的祝福GSY

I want to update a value of subcatname byk to MoterCycle How to write a query for mycodeBest RegardsGSY

推荐答案

使用位置运算符. 参考.

您的查询:

db.cats.update(
        {"catId":"2", "subcat.subcatId" : "2"},
        {$set: {"subcat.$.subcatname": "MotorCycle"}}
              )

编辑:它正在工作.这是我的测试:

It IS working. Here is my test:

> db.cats.find();
{ "_id" : ObjectId("52a15c7631282cd86d9fc1ef"), "catId" : "1", "catname" : "elect", "displayname" : "Elect", "subcat" : [     {       "subcatname" : "lap",   "subcatId" : "1",       "displayname" : "Laptops" },    {       "subcatname" : "cam",         "subcatId" : "2",       "displayname" : "Camera" } ] }
{ "_id" : ObjectId("52a15c7931282cd86d9fc1f0"), "catId" : "2", "catname" : "auto", "displayname" : "Auto", "subcat" : [       {       "subcatname" : "car",   "subcatId" : "1",       "displayname" : "Car" },        {       "subcatname" : "byk",         "subcatId" : "2",       "displayname" : "Byke" } ] }
> db.cats.update(
...         {"catId":"2", "subcat.subcatId" : "2"},
...         {$set: {"subcat.$.subcatname": "MotorCycle"}}
...               )
> db.cats.find();
{ "_id" : ObjectId("52a15c7631282cd86d9fc1ef"), "catId" : "1", "catname" : "elect", "displayname" : "Elect", "subcat" : [     {       "subcatname" : "lap",   "subcatId" : "1",       "displayname" : "Laptops" },    {       "subcatname" : "cam",         "subcatId" : "2",       "displayname" : "Camera" } ] }
{ "_id" : ObjectId("52a15c7931282cd86d9fc1f0"), "catId" : "2", "catname" : "auto", "displayname" : "Auto", "subcat" : [       {       "subcatname" : "car",   "subcatId" : "1",       "displayname" : "Car" },        {       "displayname" : "Byke",       "subcatId" : "2",       "subcatname" : "MotorCycle" } ] }
>

这篇关于如何在MongoDB中更新嵌套数组中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:35