如何使用phpMongo将Unique文档作为整体插入MongoDB中的数组?

时间:2015-10-13 09:21:40

标签: php arrays mongodb

$StringJsonVariable = '{"data": {"Parent": { "abc": [{"value": "10","code": "c102"},{"value": "20","code": "c103" }]}}';


        $JsonVariable = json_decode($StringJsonVariable,true);
        $connection = new MongoClient();
        $db = $connection -> dbvalue;
        $collection = $db -> collVal;

        foreach($JsonVariable['data']['Parent'] as $key => $value[0]){
        $val=$value[0];

         $collection->update(array('_id'=>"abc"),array('$addToSet' => array('VALUES' => array('$each' =>$val), array('upsert' => true))));}  
  

尝试使用addtoSet每个使用上面的语法。我想恭喜你   单个电话。

1 个答案:

答案 0 :(得分:0)

$JsonVariable = json_decode($StringJsonVariable,true);

$connection = new MongoClient();
$db = $connection -> dbratings;
$collection = $db -> ratings;


foreach($JsonVariable['data']['Ratings'] as $key => $value[0])
{
//$val=$value[0];
$val=$value[0];
$values=array();
foreach($val as $key=>$a)
{
array_push($values, (object)$a);

}
$collection->update(array('_id'=>"georgelenin07@gmail.com"), 
                         array('$addToSet' =>
                            array('ratings' =>
                               array('$each' => $values))), 
                         array('upsert'=>true));
}