将新值附加到数组列

时间:2015-07-05 16:38:06

标签: php mongodb

如何在MongoDb列数组中插入新值?     

    // Connect Collection
    $collection = $this->mongo_db->db->selectCollection('test');
// Remove All Document $collection->remove(); // initially Insert abcd Column inside Firstnam Lastname Value $test=array('abcd'=> array("firstname" => "Bob", "lastname" => "Jones" ) );
// Insert Value $content=$collection->insert($test);
// get Last Insert ID $newDocID = $test['_id'];
// Append New value in above abcd Array Field Column $newdata = array('$set'=> array('abcd'=> array('city'=>"Tiruppur")) );
$collection->update(array("_id" => new MongoId($newDocID)), $newdata);

    
    // Current Result
                {
                    "_id" : ObjectId("55995b0be5ffc4980b000041"),
                    "abcd" : {
                        "city" : "Tiruppur"
                    }
                }
// But Need Expect Result
{ "_id" : ObjectId("55995b0be5ffc4980b000041"), "abcd" : { "firstname" => "Bob", "lastname" => "Jones" , "city" : "Tiruppur" } }
// Please be help to Find Solution

1 个答案:

答案 0 :(得分:0)

使用点符号:

$newdata = array('$set' => array('abcd.city' => "Tiruppur"));