当前位置:Gxlcms > 数据库问题 > MongoDB操作类PHP代码

MongoDB操作类PHP代码

时间:2021-07-01 10:21:17 帮助过:25人阅读

  1. <?php
  2. include_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.‘Config‘.DIRECTORY_SEPARATOR.‘database.php‘;
  3. class MongoClass3{
  4.   static public $conn;//数据库连接
  5.   static public $db;//数据库选择
  6.   static public $collection;//结果集合
  7.   public $debug;
  8.   public function __construct($type=0){
  9.     $database=‘‘;
  10.     $database = new DATABASE_CONFIG();
  11.     if($type==1){
  12.         $mongo_master = $database->mongo_hems3;
  13.     }else if($type==2) {
  14.        $mongo_master = $database->mongo162;
  15.     }else if($type == 3) {
  16.          $mongo_master = $database->mongo_yuanchuang;
  17.     } else if($type==5) {
  18.         $mongo_master = $database->mongo_backup;
  19.     }
  20.     else if($type == 0){
  21.         $mongo_master = $database->mongo;
  22.     }
  23.     $host_master = $mongo_master[‘host‘];
  24.     $database    = $mongo_master[‘database‘];
  25.     $port        = $mongo_master[‘port‘];
  26.     $this->debug = true;
  27. //    $this->conn = new Mongo("mongodb://${username}:${password}@${host}");
  28. //    $this->conn = new Mongo("mongodb://${host_master},${host_slave}",array("replicaSet" => "shard"));
  29.     try{
  30.         $this->conn = new Mongo("mongodb://${host_master}:${port}");
  31.     }catch(Exception $e){
  32.         $str = $e."\r\n".date(‘Y-m-d H:i:s‘,time())." 连接mongo失败\r\n";
  33.         $this->getMongoLogo($str);
  34.         return false;
  35.     }
  36. //     $this->conn=$db->admin;
  37. //     $this->conn->authenticate(‘root‘, ‘123456‘);
  38.     $this->db = $this->conn->{$database};
  39.   }
  40.     /**
  41.      * Description 查询配置表hems_basic记录数 可按条件查询记录数
  42.      * @param Array $conditions  可为空  条件格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示条件之间是AND关系
  43.      * @return int  $count
  44.      *
  45.      */
  46.     public function getCount($collection,$conditions = array()){
  47.         $this->collection = $this->db->{$collection};
  48.         $count = $this->collection->find($conditions)->count();
  49.         return $count;
  50.     }
  51.     /**
  52.      * Description 根据条件查询配置表hems_basic文档
  53.      * @param  Array $conditions  可为空    条件格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示条件之间是AND关系
  54.      * @return Array $res  返回的结果是mongo对象需要遍历才能取出数据
  55.      *
  56.      */
  57.     public function getData($collection,$conditions = array(),$field=array(),$order=array(),$start=0,$pernum=20){
  58.         $this->collection = $this->db->{$collection};
  59.         if(empty($collection)){
  60.             return false;
  61.         }
  62.         try {
  63.             if($start==1&&$pernum==0){
  64.                 if($order){
  65.                     $res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order);
  66.                 }else{
  67.                     $res = $this->collection->find($conditions,$field)->slaveOkay(true);
  68.                 }
  69.             }else{
  70.                 if($order){
  71.                     $res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order)->skip($start)->limit($pernum);
  72.                 }else{
  73.                     $res = $this->collection->find($conditions,$field)->slaveOkay(true)->skip($start)->limit($pernum);
  74.                 }
  75.             }
  76.         }catch(Exception $e){
  77.             $res = array();
  78.             $str =  date(‘Y-m-d H:i:s‘,time())." 从集合".$collection."获取数据\r\n条件为:\r\n";
  79.             $str1= ‘‘;
  80.             foreach($conditions as $key => $val){
  81.                 $str1.= $key."------------".$val."\r\n";
  82.             }
  83.             $this->getMongoLogo($str.$str1."失败\r\n");
  84.             return false;
  85.         }
  86.         try{
  87.             $res->rewind();
  88.         }catch(Exception $e){
  89.             $str =  date(‘Y-m-d H:i:s‘,time())." 从集合".$collection."获取数据\r\n条件为:\r\n";
  90.             $str1= ‘‘;
  91.             foreach($conditions as $key => $val){
  92.                 $str1.= $key."------------".$val."\r\n";
  93.             }
  94.             $this->getMongoLogo($str.$str1."失败\r\n");
  95.             return false;
  96.         }
  97.         if($res->valid()){
  98.             return iterator_to_array($res,true);
  99.         }else{
  100.             return false;
  101.         }
  102.     }
  103.     /**
  104.      * Description 根据条件查询配置表hems_basic文档
  105.      * @param  Array $conditions  可为空    条件格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示条件之间是AND关系
  106.      * @return Array $res  返回的结果是mongo对象需要遍历才能取出数据
  107.      *
  108.      */
  109.     public function getDataOr($collection,$conditions){
  110.         $this->collection = $this->db->{$collection};
  111.         $res = $this->collection->find(array(‘$or‘=>$conditions));
  112.         return $res;
  113.     }
  114.     /**
  115.      * Description 计算数据表中所有文档数(按条件计算,无条件计算整个集合的总数)
  116.      * @param Array $conditions 可为空    条件格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示条件之间是AND关系
  117.      * @return int  $count
  118.      */
  119.     public function getIdDataCount($collection,$conditions = array()){
  120.         $this->collection = $this->db->{$collection};
  121.         $count = $this->collection->find($conditions)->count();
  122.         return $count;
  123.     }
  124.     /**
  125.      * Description 根据条件或者配置表中的_id到hems_data数据表中取出对应所有文档文档
  126.      * @param Array $conditions 可为空   条件格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示条件之间是AND关系
  127.      * @return Array $res  返回的结果是mongo对象需要遍历才能取出数据
  128.      */
  129.     public function getIdData($collection,$conditions){
  130.         $this->collection = $this->db->{$collection};
  131.         $res = $this->collection->find($conditions);
  132.         return $res;
  133.     }
  134.     /**
  135.      * Description 根据条件修改数据表文档
  136.      * @param Array $conditions $data   $conditions格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示多个条件并列
  137.      *                                  $data格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示修改的字段
  138.      */
  139.     public function editData($collection,$conditions,$data){
  140.         $this->collection = $this->db->{$collection};
  141.         if($this->collection->update($conditions ,array(‘$set‘=>$data),array(‘multiple‘=>1))){
  142.             $code = 1;
  143.             $msg  = ‘修改成功!‘;
  144.         }else{
  145.             $code = 0;
  146.             $msg  = ‘修改失败!‘;
  147.         }
  148.         return array(‘code‘=>$code,‘msg‘=>$msg);
  149.     }
  150.     
  151.     /*
  152.      * Description 根据条件删除数据表文档
  153.      * @param Array $conditions    $conditions格式为数组  array(‘字段1‘=>‘字段值1‘,‘字段2‘=>‘字段值2‘) 表示多个条件并列
  154.      */
  155.     public function delData($collection,$conditions){
  156.         $this->collection = $this->db->{$collection};
  157.         if($this->collection->remove($conditions)){
  158.             $code = 1;
  159.             $msg  = ‘删除成功!‘;
  160.         }else{
  161.             $code = 0;
  162.             $msg  = ‘删除失败!‘;
  163.         }
  164.         return array(‘code‘=>$code,‘msg‘=>$msg);
  165.    }
  166.     /*
  167.      * Description 插入操作
  168.      * 根据主键_id判断,如果该记录已经存在则
  169.      */
  170.     public function save($collection,$field,$flag=0){
  171.         $this->collection = $this->db->{$collection};
  172.         $log = "";
  173.         foreach($field as $kdata => $vdata){
  174.             $log .= $kdata.":".$vdata.",";
  175.         }
  176.         $log = trim($log,",");
  177.         $newLog = "\r\n-------\r\ntime:".date("Y-m-d H:i:s")."\r\ndata".$log;
  178.         $oldLog = file_get_contents("../tmp/mongoLog.txt");
  179.         @file_put_contents("../tmp/mongoLog.txt",$oldLog.$newLog);
  180.         if($flag){
  181.           $result = $this->collection->save($field,array(‘safe‘=>1));
  182.         }else{
  183.           $result = $this->collection->insert($field,array(‘safe‘=>1));
  184.         }
  185.         return $result;
  186.     }
  187.    
  188. }
  189. ?>

本文出自 “PHP程序猿” 博客,请务必保留此出处http://okowo.blog.51cto.com/4923464/1690710

MongoDB操作类PHP代码

标签:mongo;php;

人气教程排行