MongoDB操作類PHPコード

8036 ワード

<?php
include_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'Config'.DIRECTORY_SEPARATOR.'database.php';
class MongoClass3{
  static public $conn;//     
  static public $db;//     
  static public $collection;//    
  public $debug;
  public function __construct($type=0){
    $database='';
    $database = new DATABASE_CONFIG();
    if($type==1){
        $mongo_master = $database->mongo_hems3;
    }else if($type==2) {
       $mongo_master = $database->mongo162;
    }else if($type == 3) {
         $mongo_master = $database->mongo_yuanchuang;
    } else if($type==5) {
        $mongo_master = $database->mongo_backup;
    }
    else if($type == 0){
        $mongo_master = $database->mongo;
    }
    $host_master = $mongo_master['host'];
    $database    = $mongo_master['database'];
    $port        = $mongo_master['port'];
    $this->debug = true;
//    $this->conn = new Mongo("mongodb://${username}:${password}@${host}");
//    $this->conn = new Mongo("mongodb://${host_master},${host_slave}",array("replicaSet" => "shard"));
    try{
        $this->conn = new Mongo("mongodb://${host_master}:${port}");
    }catch(Exception $e){
        $str = $e."\r
".date('Y-m-d H:i:s',time())."  mongo \r
";         $this->getMongoLogo($str);         return false;     } //     $this->conn=$db->admin; //     $this->conn->authenticate('root', '123456');     $this->db = $this->conn->{$database};   }     /**      * Description  hems_basic        * @param Array $conditions        array(' 1'=>' 1',' 2'=>' 2')  AND      * @return int  $count      *      */     public function getCount($collection,$conditions = array()){         $this->collection = $this->db->{$collection};         $count = $this->collection->find($conditions)->count();         return $count;     }     /**      * Description  hems_basic      * @param  Array $conditions          array(' 1'=>' 1',' 2'=>' 2')  AND      * @return Array $res   mongo      *      */     public function getData($collection,$conditions = array(),$field=array(),$order=array(),$start=0,$pernum=20){         $this->collection = $this->db->{$collection};         if(empty($collection)){             return false;         }         try {             if($start==1&&$pernum==0){                 if($order){                     $res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order);                 }else{                     $res = $this->collection->find($conditions,$field)->slaveOkay(true);                 }             }else{                 if($order){                     $res = $this->collection->find($conditions,$field)->slaveOkay(true)->sort($order)->skip($start)->limit($pernum);                 }else{                     $res = $this->collection->find($conditions,$field)->slaveOkay(true)->skip($start)->limit($pernum);                 }             }         }catch(Exception $e){             $res = array();             $str =  date('Y-m-d H:i:s',time())."  ".$collection." \r
:\r
";             $str1= '';             foreach($conditions as $key => $val){                 $str1.= $key."------------".$val."\r
";             }             $this->getMongoLogo($str.$str1." \r
");             return false;         }         try{             $res->rewind();         }catch(Exception $e){             $str =  date('Y-m-d H:i:s',time())."  ".$collection." \r
:\r
";             $str1= '';             foreach($conditions as $key => $val){                 $str1.= $key."------------".$val."\r
";             }             $this->getMongoLogo($str.$str1." \r
");             return false;         }         if($res->valid()){             return iterator_to_array($res,true);         }else{             return false;         }     }     /**      * Description  hems_basic      * @param  Array $conditions          array(' 1'=>' 1',' 2'=>' 2')  AND      * @return Array $res   mongo      *      */     public function getDataOr($collection,$conditions){         $this->collection = $this->db->{$collection};         $res = $this->collection->find(array('$or'=>$conditions));         return $res;     }     /**      * Description  ( , )      * @param Array $conditions         array(' 1'=>' 1',' 2'=>' 2')  AND      * @return int  $count      */     public function getIdDataCount($collection,$conditions = array()){         $this->collection = $this->db->{$collection};         $count = $this->collection->find($conditions)->count();         return $count;     }     /**      * Description  _id hems_data      * @param Array $conditions        array(' 1'=>' 1',' 2'=>' 2')  AND      * @return Array $res   mongo      */     public function getIdData($collection,$conditions){         $this->collection = $this->db->{$collection};         $res = $this->collection->find($conditions);         return $res;     }     /**      * Description       * @param Array $conditions $data   $conditions   array(' 1'=>' 1',' 2'=>' 2')       *                                  $data   array(' 1'=>' 1',' 2'=>' 2')       */     public function editData($collection,$conditions,$data){         $this->collection = $this->db->{$collection};         if($this->collection->update($conditions ,array('$set'=>$data),array('multiple'=>1))){             $code = 1;             $msg  = ' !';         }else{             $code = 0;             $msg  = ' !';         }         return array('code'=>$code,'msg'=>$msg);     }          /*      * Description       * @param Array $conditions    $conditions   array(' 1'=>' 1',' 2'=>' 2')       */     public function delData($collection,$conditions){         $this->collection = $this->db->{$collection};         if($this->collection->remove($conditions)){             $code = 1;             $msg  = ' !';         }else{             $code = 0;             $msg  = ' !';         }         return array('code'=>$code,'msg'=>$msg);    }     /*      * Description       *  _id ,      */     public function save($collection,$field,$flag=0){         $this->collection = $this->db->{$collection};         $log = "";         foreach($field as $kdata => $vdata){             $log .= $kdata.":".$vdata.",";         }         $log = trim($log,",");         $newLog = "\r
-------\r
time:".date("Y-m-d H:i:s")."\r
data".$log;         $oldLog = file_get_contents("../tmp/mongoLog.txt");         @file_put_contents("../tmp/mongoLog.txt",$oldLog.$newLog);         if($flag){           $result = $this->collection->save($field,array('safe'=>1));         }else{           $result = $this->collection->insert($field,array('safe'=>1));         }         return $result;     }     } ?>