PHP 操作mongodb api大部分方法

<?php
$connection = new MongoClient(); // connect
$collection = $connection->database->aaa;  
$doc = array(  
"name"=>"MongoDB",  
"type"=>"database",  
"count"=>1,  
"info"=>(object)array("x"=>203, "y"=>102),  
"versions"=>array("0.9.7", "0.9.8", "0.9.9")  
);  
//$collection->insert($doc);  
$document = $collection->findOne();  
 $opt = array(
      'fsync' => 1 //默认为 false , false时当日志配置开启时和 j 参数的功能类似 否则写操作会等到数据完全同步到硬盘后...true则忽略执行结果
  );

//$ret = $collection->insert( array('pid'=>2 , 'content'=>'test123') , $opt );
$ret =$collection->update( array('pid'=> 2 ), array('$set'=> array('content'=>'123456789') ) );
 //删除某个属性$unset
//$ret = $collection->update( array('pid'=> 2 ), array('$unset'=> array('content'=>1 ,'contents'=>1) ) );
//为某个属性 新增/更新 一条记录 若属性不存在则新增属性
 $ret = $collection->update( array('pid'=> 2 ), array('$push'=> array('contents'=> array('v'=>1) ) ) );
 
 //批量增加数据
    //$ret = $collection->update( array('pid'=> 2 ), array('$pushAll'=> array('contents'=> array(1,2,3,4) ) ) );
    //$addToSet
    //和push功能类似 与$push不同的是$addToSet会保证元素的唯一性,防止重复添加
    //$ret = $collection->update( array('pid'=> 2 ), array('$addToSet'=> array('contents'=> 1 ) ) );
    //$pop
    //删除字段数组中的第一条记录 。mongodb 1.1及以后的版本可以用 2或-2来删除两条,
    //$ret = $collection->update( array('pid'=> 2 ), array('$pop'=> array('contents'=> -1 ) ) );
    //$pull   index
    //$ret = $collection->update( array('pid'=> 2 ), array('$pull'=> array( 'contents' => 2 ) ) );
  //$ret = $collection->update( array('pid'=> 2 ), array('$pullAll'=> array( 'contents' => array(1,2,3,4) ) ) );
    //$inc  $代表子项中的自身
    //$ret = $collection->update( array('contents'=> 3 ), array('$inc'=> array('contents.$' => 100 )  ) );
    //$代表子项中的匹配记录自身
     //删除
        //$ret = $collection->update( array('contents'=> 8 ), array('$unset'=> array('contents.$' => 1 )  ) );
        //修改
        //$ret = $collection->update( array('contents'=> 2 ), array('$set'=> array('contents.$' => 11 )  ) );
        //添加 PS:更新的节点要是数组类型
        //  $ret = $collection->update( array('contents.v'=> 34 ), array('$push'=> array('contents.$.test' => array('t1'=>1 ,'t2'=>2) ) ));
    
/*删除*/ 
    //$ret = $collection->remove( array('pid'=> 2 ) );
    //只删除一条
    //$ret = $collection->remove( array('pid'=> 2 ) , array("justOne" => true));
/*查询*/
 //$ret = $collection->find( array('pid'=>2 , 'content'=>'test123') );
 //子查询
    //$ret = $collection->findOne( array('pid'=>2 , 'contents'=>  1 ) );
    //$ret = $collection->findOne( array('pid'=>2 , 'contents.t'=>  5 ) );
 //获取总数
//$t = $ret->count();
//越过多少
//$ret =  $collection->find( array('pid'=>2) )->skip( 10 );
//排序
//$ret =  $collection->find( array('pid'=>2) )->sort(array("a" => 1));
//返回字段
//$ret =  $collection->find( array('pid'=>2), array('content') );
//$ret =  $collection->find( array('pid'=>2), array('content' => 0 ) ); //忽略字段
//返回对象  php >= 5.1  iterator_to_array  
//var_dump( iterator_to_array( $ret ) );
//否则
//foreach( $ret  as $v){
//    var_dump( $v );
//}
//$ret =  $collection->findOne(array('pid'=>2 , 'content'=>'test123'));
// 直接返回数组
var_dump($document);  


http://www.cnblogs.com/glory-jzx/p/4054792.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值