initphp mongo

  1. /********************************************************************************* php

  2.  * InitPHP 2.0 國產PHP開發框架  Dao-Nosql-Mongo  sql

  3.  *------------------------------------------------------------------------------- mongodb

  4.  * 版權全部: CopyRight By initphp.com 數據庫

  5.  * 您能夠自由使用該源碼,可是在使用過程當中,請保留做者信息。尊重他人勞動成果就是尊重本身 服務器

  6.  *------------------------------------------------------------------------------- 框架

  7.  * $Author:zhuli this

  8.  * $Dtime:2011-10-09  spa

  9. ***********************************************************************************/  server

  10. class mongoInit {  對象

  11.   

  12.     private $mongo//mongo對象  

  13.     private $db//db mongodb對象數據庫  

  14.     private $collection//集合,至關於數據表   

  15.       

  16.     /** 

  17.      * 初始化Mongo 

  18.      * $config = array( 

  19.      * 'server' => ‘127.0.0.1' 服務器地址 

  20.      * ‘port’   => '27017' 端口地址 

  21.      * ‘option’ => array('connect' => true) 參數 

  22.      * 'db_name'=> 'test' 數據庫名稱 

  23.      * ‘username’=> 'zhuli' 數據庫用戶名 

  24.      * ‘password’=> '123456' 數據庫密碼 

  25.      * ) 

  26.      * Enter description here ... 

  27.      * @param unknown_type $config 

  28.      */  

  29.     public function init($config = array()) {  

  30.         if ($config['server'] == '')  $config['server'] = '127.0.0.1';  

  31.         if ($config['port'] == '')  $config['port'] = '27017';  

  32.         if (!$config['option']) $config['option'] = array('connect' => true);  

  33.         $server = 'mongodb://' . $config['server'] . ':' . $config['port'];  

  34.         $this->mongo = new Mongo($server$options);  

  35.         if ($config['db_name'] == ''$config['db_name'] = 'test';  

  36.         $this->db = $this->mongo->selectDB($config['db_name']);  

  37.         if ($config['username'] != '' && $config['password'] != '')   

  38.             $this->db->authenticate($config['username'], $config['password']);  

  39.     }  

  40.       

  41.     /** 

  42.      * 選擇一個集合,至關於選擇一個數據表 

  43.      * @param string $collection 集合名稱 

  44.      */  

  45.     public function selectCollection($collection) {  

  46.         return $this->collection = $this->db->selectCollection($collection);  

  47.     }  

  48.       

  49.     /** 

  50.      * 新增數據 

  51.      * @param array $data 須要新增的數據 例如:array('title' => '1000', 'username' => 'xcxx') 

  52.      * @param array $option 參數 

  53.      */  

  54.     public function insert($data$option = array()) {  

  55.         return $this->collection->insert($data$option);  

  56.     }  

  57.       

  58.     /** 

  59.      * 批量新增數據 

  60.      * @param array $data 須要新增的數據 例如:array(0=>array('title' => '1000', 'username' => 'xcxx')) 

  61.      * @param array $option 參數 

  62.      */  

  63.     public function batchInsert($data$option = array()) {  

  64.         return $this->collection->batchInsert($data$option);  

  65.     }  

  66.       

  67.     /** 

  68.      * 保存數據,若是已經存在在庫中,則更新,不存在,則新增 

  69.      * @param array $data 須要新增的數據 例如:array(0=>array('title' => '1000', 'username' => 'xcxx')) 

  70.      * @param array $option 參數 

  71.      */  

  72.     public function save($data$option = array()) {  

  73.         return $this->collection->save($data$option);  

  74.     }  

  75.       

  76.     /** 

  77.      * 根據條件移除 

  78.      * @param array $query  條件 例如:array(('title' => '1000')) 

  79.      * @param array $option 參數 

  80.      */  

  81.     public function remove($query$option = array()) {  

  82.         return $this->collection->remove($query$option);  

  83.     }  

  84.       

  85.     /** 

  86.      * 根據條件更新數據 

  87.      * @param array $query  條件 例如:array(('title' => '1000')) 

  88.      * @param array $data   須要更新的數據 例如:array(0=>array('title' => '1000', 'username' => 'xcxx')) 

  89.      * @param array $option 參數 

  90.      */  

  91.     public function update($query$data$option = array()) {  

  92.         return $this->collection->update($query$data$option);  

  93.     }  

  94.       

  95.     /** 

  96.      * 根據條件查找一條數據 

  97.      * @param array $query  條件 例如:array(('title' => '1000')) 

  98.      * @param array $fields 參數 

  99.      */  

  100.     public function findOne($query$fields = array()) {  

  101.         return $this->collection->findOne($query$fields);  

  102.     }  

  103.       

  104.     /** 

  105.      * 根據條件查找多條數據 

  106.      * @param array $query 查詢條件 

  107.      * @param array $sort  排序條件 array('age' => -1, 'username' => 1) 

  108.      * @param int   $limit 頁面 

  109.      * @param int   $limit 查詢到的數據條數 

  110.      * @param array $fields返回的字段 

  111.      */  

  112.     public function find($query$sort = array(), $skip = 0, $limit = 0, $fields = array()) {  

  113.         $cursor = $this->collection->find($query$fields);  

  114.         if ($sort)  $cursor->sort($sort);  

  115.         if ($skip)  $cursor->skip($skip);  

  116.         if ($limit$cursor->limit($limit);  

  117.         return iterator_to_array($cursor);  

  118.     }  

  119.       

  120.     /** 

  121.      * 數據統計 

  122.      */  

  123.     public function count() {  

  124.         return $this->collection->count();  

  125.     }  

  126.       

  127.     /** 

  128.      * 錯誤信息 

  129.      */  

  130.     public function error() {  

  131.         return $this->db->lastError();  

  132.     }  

  133.       

  134.     /** 

  135.      * 獲取集合對象 

  136.      */  

  137.     public function getCollection() {  

  138.         return $this->collection;  

  139.     }  

  140.       

  141.     /** 

  142.      * 獲取DB對象 

  143.      */  

  144.     public function getDb() {  

  145.         return $this->db;  

  146.     }  

  147.       

  148.       

  149. }  

相關文章
相關標籤/搜索