简体   繁体   中英

update nested array mongoDB PHP

i actually have a issue with mongodb im trying to update/add a value in a nested array.

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

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