当前位置: 首页 > Linux

使用

时间:2023-04-06 23:03:57 Linux

的PHP7mongoDB扩展最近在做的项目需要把PHP5.6升级到PHP7.0。用过PHP-mongo扩展的同学应该知道,PHP7.0的mongodb扩展和PHP5.6的mongo扩展是完全不兼容的。如何更改php-mongodb使用它。下面直接说明各种方法的使用:1.mongodb连接:privatefunctionconnect($confArr){try{$connStr="mongodb://".$confArr['主机']。“:”。$confArr['端口']。“/”。$confArr['db_name'];$options=array('username'=>$confArr['username'],'password'=>$confArr['password'],'readPreference'=>$confArr['read_preference'],'connectTimeoutMS'=>intval($confArr['connect_timeout_ms']),'socketTimeoutMS'=>intval($confArr['socket_timeout_ms']),);$mc=newMongoDB\Driver\Manager($connStr,$options);返回$mc;}catch(Exception$e){返回false;}}2.查询查找:publicfunctionfind($query=array(),$fields=array(),$collection,$sort=array(),$limit=0,$skip=0){$conn=$这个->连接();如果(空($conn)){返回假;}尝试{$data=array();$选项=数组();如果(!empty($query)){$options['projection']=array_fill_keys($fields,1);}if(!empty($sort)){$options['sort']=$sort;}if(!empty($limit)){$options['skip']=$skip;$options['limit']=$limit;}$mongoQuery=newMongoDB\Driver\Query($query,$options);$readPreference=newMongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);$cursor=$conn->executeQuery($collection,$mongoQuery,$readPreference);foreach($cursoras$value){$data[]=(array)$value;}返回$数据;}catch(Exception$e){//记录错误日志}returnfalse;}3.插入操作insert:publicfunctioninsert($addArr,$collection){if(empty($addArr)||!is_array($addArr)){返回错误;}$conn=$this->connect();如果(空($conn)){返回假;}try{$bulk=newMongoDB\Driver\BulkWrite();$批量->插入($addArr);$writeConcern=newMongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,6000);$result=$conn->executeBulkWrite($collection,$bulk,$writeConcern);如果($result->getInsertedCount()){返回真;}}catch(Exception$e){//记录错误日志}returnfalse;}4.删除delete:publicfunctiondelete($whereArr,$options=array(),$collection){if(empty($whereArr)){返回假;}if(!isset($options['justOne'])){$options=array('justOne'=>false,);}$conn=$this->connect();如果(空($conn)){返回假;}try{$bulk=newMongoDB\Driver\BulkWrite();$bulk->delete($whereArr,$options);$writeConcern=newMongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,30000);$result=$conn->executeBulkWrite($collection,$bulk,$writeConcern);返回真;}catch(Exception$e){//记录错误日志}returnfalse;}5.执行命令操作:privatefunctioncommand($params,$dbName){$conn=$this->connect();如果(空($conn)){返回假;}尝试{$cmd=newMongoDB\Driver\Command($params);$result=$conn->executeCommand($dbName,$cmd);返回$结果;}catch(Exception$e){//记录错误}returnfalse;}6.统计计数:publicfunctioncount($query,$collection){try{$cmd=array('count'=>$collection,'query'=>$查询,);$res=$this->command($cmd);$result=$res->toArray();返回$result[0]->n;}catch(Exception$e){//记录错误}returnfalse;}7.聚合distinct:publicfunctiondistinct($key,$where,$collection){try{$cmd=array('distinct'=>$collection,'key'=>$key,'query'=>$where,);$res=$this->command($cmd);$result=$res->toArray();返回$result[0]->值;}catch(Exception$e){//记录错误}returnfalse;}8.aggregate操作:publicfunctionaggregate($where,$group,$collection){try{$cmd=array('aggregate'=>$collection,'pipeline'=>array(array('$match'=>$where,),array('$group'=>$group,),),'explain'=>false,);$res=$this->command($cmd);如果(!$res){返回假;$result=$res->toArray();返回$result[0]->总计;}catch(Exception$e){//记录错误}returnfalse;}