使用php在mongodb中插入数组

时间:2012-04-09 05:55:56

标签: php mongodb

如何使用php在mongodb中插入内部数组我在sheel中运行此命令然后运行

db.domain_data.update({"email":"rohan@walkover.in","url.tlds":".com"},{'$addToSet':{"url.$.domain":{"name":"rohan","status":"yes"}}}})

但我在php中编写此代码然后它无效

<?
    $domain=array("name"=>"asvdsfs","status"=>"yes");
    $condition=array("email"=>"rohan@walkover.in","url"=>array("tlds"=>".com"));
    $arr=array('$addToSet'=>array("url.$.domain"=>array($domain)));
    //$arr=array('$addToSet'=>array("url"=>array("$"=>array("domain"=>array($domain)))));
    //Above line also not working.
    $status=$db->domain_data->update($condition,array($arr));
?>

如何编写上面运行正确的PHP代码?

1 个答案:

答案 0 :(得分:0)

我想你可能有额外的数组

<?     
      $domain=array("name"=>"asvdsfs","status"=>"yes");     
      $condition=array("email"=>"rohan@walkover.in","url.tlds"=>".com");//1 more change//,"url"=>array("tlds"=>".com")          
      $arr=array('$addToSet'=>array("url.$.domain"=>$domain));    // change this line 
      //$arr=array('$addToSet'=>array("url"=>array("$"=>array("domain"=>array($domain)))));     
      //Above line also not working.     
      $status=$db->domain_data->update($condition,$arr);   // change this line 

?>