MongoDB操作類PHP代碼

來源:互聯網
上載者:User

標籤:mongo;php;

<?phpinclude_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\n".date(‘Y-m-d H:i:s‘,time())." 串連mongo失敗\r\n";        $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\n條件為:\r\n";            $str1= ‘‘;            foreach($conditions as $key => $val){                $str1.= $key."------------".$val."\r\n";            }            $this->getMongoLogo($str.$str1."失敗\r\n");            return false;        }        try{            $res->rewind();        }catch(Exception $e){            $str =  date(‘Y-m-d H:i:s‘,time())." 從集合".$collection."擷取資料\r\n條件為:\r\n";            $str1= ‘‘;            foreach($conditions as $key => $val){                $str1.= $key."------------".$val."\r\n";            }            $this->getMongoLogo($str.$str1."失敗\r\n");            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\n-------\r\ntime:".date("Y-m-d H:i:s")."\r\ndata".$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;    }   }?>

本文出自 “PHP程式猿” 部落格,請務必保留此出處http://okowo.blog.51cto.com/4923464/1690710

MongoDB操作類PHP代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.