如何在MongoDB中更新嵌套数组中的数据

时间:2013-12-05 13:58:09

标签: mongodb

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

{

    "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 如何为mycode编写查询 最好的祝福 GSY

1 个答案:

答案 0 :(得分:3)

使用位置运算符。 Reference

您的查询:

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