dtcd27183
dtcd27183
2015-10-13 09:21

如何使用phpMongo将Unique文档作为整体插入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))));}  

Tried to use addtoSet Each using above syntax. I want to upsert in
single call.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douwen7603 douwen7603 6年前
    $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));
    }
    
    点赞 评论 复制链接分享