简体   繁体   English

更新嵌套数组mongoDB PHP

[英]update nested array mongoDB PHP

i actually have a issue with mongodb im trying to update/add a value in a nested array. 我实际上在尝试更新/添加嵌套数组中的值时遇到了mongodb im的问题。

{
    "_id" : ObjectId("56c37e98aff662100900002a"),
    "name" : "michell",
    "game" : [{
        "name" : "GTA",
        "badges" : [{
            "name" : "pacifist"
        }, {
            "name" : "killemall"
        }]
    }]
}

you can find below the way i tried but actually it just rewrite the badges array of create new game array 您可以在下面尝试的方式下找到它,但实际上它只是重写了创建新游戏数组的徽章数组

 $collection->update(array('_id' =>new MongoId($id),'jeux.name'=>$name), array('$set' => array('jeux'=>array('name' => $name,'badges'=>array('name'=>$badge)))));

i can't find the issue here if you could help me 如果您能帮助我,我在这里找不到问题

Well i found a solution i don't know if its the best one but it seems like it work 好吧,我找到了一个我不知道它是否是最好的解决方案,但似乎可行

 $collection->update(array('_id' =>new MongoId($id),'jeux.name'=>$name), array('$push' => array('jeux.$.badges'=>array('name'=>$badge))));

im using the $ in jeux.$.badges 我在jeux中使用$。

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

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