PHP操作Mongodb API 及使用类 封装好的MongoDB操作类

来源:http://www.itokit.com/2012/0427/73656.html

  1. <?php     
  2. /*** Mongodb类** examples:     
  3. * $mongo = new HMongodb("127.0.0.1:11223");   
  4. * $mongo->selectDb("test_db");   
  5. * 创建索引   
  6. * $mongo->ensureIndex("test_table", array("id"=>1), array('unique'=>true));   
  7. * 获取表的记录   
  8. * $mongo->count("test_table");   
  9. * 插入记录   
  10. * $mongo->insert("test_table", array("id"=>2, "title"=>"asdqw"));   
  11. * 更新记录   
  12. * $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"));   
  13. * 更新记录-存在时更新,不存在时添加-相当于set   
  14. * $mongo->update("test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"),array("upsert"=>1));  
  15. * 查找记录   
  16. * $mongo->find("c", array("title"=>"asdqw"), array("start"=>2,"limit"=>2,"sort"=>array("id"=>1)))  
  17. * 查找一条记录   
  18. * $mongo->findOne("$mongo->findOne("ttt", array("id"=>1))", array("id"=>1));   
  19. * 删除记录   
  20. * $mongo->remove("ttt", array("title"=>"bbb"));   
  21. * 仅删除一条记录   
  22. * $mongo->remove("ttt", array("title"=>"bbb"), array("justOne"=>1));   
  23. * 获取Mongo操作的错误信息   
  24. * $mongo->getError();   
  25. */     
  26.      
  27. class HMongodb {     
  28.      
  29.     //Mongodb连接     
  30.     var $mongo;     
  31.      
  32.     var $curr_db_name;     
  33.     var $curr_table_name;     
  34.     var $error;     
  35.      
  36.     /**   
  37.     * 构造函数   
  38.     * 支持传入多个mongo_server(1.一个出问题时连接其它的server 2.自动将查询均匀分发到不同server)   
  39.     *   
  40.     * 参数:   
  41.     * $mongo_server:数组或字符串-array("127.0.0.1:1111", "127.0.0.1:2222")-"127.0.0.1:1111"   
  42.     * $connect:初始化mongo对象时是否连接,默认连接   
  43.     * $auto_balance:是否自动做负载均衡,默认是   
  44.     *   
  45.     * 返回值:   
  46.     * 成功:mongo object   
  47.     * 失败:false   
  48.     */     
  49.     function __construct($mongo_server$connect=true, $auto_balance=true)     
  50.     {     
  51.         if (is_array($mongo_server))     
  52.         {     
  53.             $mongo_server_num = count($mongo_server);     
  54.             if ($mongo_server_num > 1 && $auto_balance)     
  55.             {     
  56.                 $prior_server_num = rand(1, $mongo_server_num);     
  57.                 $rand_keys = array_rand($mongo_server,$mongo_server_num);     
  58.                 $mongo_server_str = $mongo_server[$prior_server_num-1];     
  59.                 foreach ($rand_keys as $key)     
  60.                 {     
  61.                     if ($key != $prior_server_num - 1)     
  62.                     {     
  63.                         $mongo_server_str .= ',' . $mongo_server[$key];     
  64.                     }     
  65.                 }     
  66.             }     
  67.             else     
  68.             {     
  69.                 $mongo_server_str = implode(','$mongo_server);     
  70.             }                  }     
  71.         else     
  72.         {     
  73.             $mongo_server_str = $mongo_server;     
  74.         }     
  75.         try {     
  76.             $this->mongo = new Mongo($mongo_serverarray('connect'=>$connect));     
  77.         }     
  78.         catch (MongoConnectionException $e)     
  79.         {     
  80.             $this->error = $e->getMessage();     
  81.             return false;     
  82.         }     
  83.     }     
  84.      
  85.     function getInstance($mongo_server$flag=array())     
  86.     {     
  87.         static $mongodb_arr;     
  88.         if (emptyempty($flag['tag']))     
  89.         {     
  90.             $flag['tag'] = 'default';          }     
  91.         if (isset($flag['force']) && $flag['force'] == true)     
  92.         {     
  93.             $mongo = new HMongodb($mongo_server);     
  94.             if (emptyempty($mongodb_arr[$flag['tag']]))     
  95.             {     
  96.                 $mongodb_arr[$flag['tag']] = $mongo;     
  97.             }     
  98.             return $mongo;     
  99.         }     
  100.         else if (isset($mongodb_arr[$flag['tag']]) && is_resource($mongodb_arr[$flag['tag']]))     
  101.         {     
  102.             return $mongodb_arr[$flag['tag']];     
  103.         }     
  104.         else     
  105.         {     
  106.             $mongo = new HMongodb($mongo_server);     
  107.             $mongodb_arr[$flag['tag']] = $mongo;     
  108.             return $mongo;                  }          }     
  109.      
  110.     /**   
  111.     * 连接mongodb server   
  112.     *   
  113.     * 参数:无   
  114.     *   
  115.     * 返回值:   
  116.     * 成功:true   
  117.     * 失败:false   
  118.     */     
  119.     function connect()     
  120.     {     
  121.         try {     
  122.             $this->mongo->connect();     
  123.             return true;     
  124.         }     
  125.         catch (MongoConnectionException $e)     
  126.         {     
  127.             $this->error = $e->getMessage();     
  128.             return false;     
  129.         }     
  130.     }     
  131.      
  132.     /**   
  133.     * select db   
  134.     *   
  135.     * 参数:$dbname   
  136.     *   
  137.     * 返回值:无   
  138.     */     
  139.     function selectDb($dbname)     
  140.     {     
  141.         $this->curr_db_name = $dbname;     
  142.     }     
  143.      
  144.     /**   
  145.     * 创建索引:如索引已存在,则返回。   
  146.     *   
  147.     * 参数:   
  148.     * $table_name:表名   
  149.     * $index:索引-array("id"=>1)-在id字段建立升序索引   
  150.     * $index_param:其它条件-是否唯一索引等   
  151.     *   
  152.     * 返回值:   
  153.     * 成功:true   
  154.     * 失败:false   
  155.     */     
  156.     function ensureIndex($table_name$index$index_param=array())     
  157.     {     
  158.         $dbname = $this->curr_db_name;     
  159.         $index_param['safe'] = 1;     
  160.         try {     
  161.             $this->mongo->$dbname->$table_name->ensureIndex($index$index_param);     
  162.             return true;     
  163.         }     
  164.         catch (MongoCursorException $e)     
  165.         {     
  166.             $this->error = $e->getMessage();     
  167.             return false;     
  168.         }     
  169.     }     
  170.      
  171.     /**   
  172.     * 插入记录   
  173.     *   
  174.     * 参数:   
  175.     * $table_name:表名   
  176.     * $record:记录   
  177.     *   
  178.     * 返回值:   
  179.     * 成功:true   
  180.     * 失败:false   
  181.     */     
  182.     function insert($table_name$record)     
  183.     {     
  184.         $dbname = $this->curr_db_name;     
  185.         try {     
  186.             $this->mongo->$dbname->$table_name->insert($recordarray('safe'=>true));     
  187.             return true;     
  188.         }     
  189.         catch (MongoCursorException $e)     
  190.         {     
  191.             $this->error = $e->getMessage();     
  192.             return false;     
  193.         }     
  194.     }     
  195.      
  196.     /**   
  197.     * 查询表的记录数   
  198.     *   
  199.     * 参数:   
  200.     * $table_name:表名   
  201.     *   
  202.     * 返回值:表的记录数   
  203.     */     
  204.     function count($table_name)     
  205.     {     
  206.         $dbname = $this->curr_db_name;     
  207.         return $this->mongo->$dbname->$table_name->count();     
  208.     }     
  209.      
  210.     /**   
  211.     * 更新记录   
  212.     *   
  213.     * 参数:   
  214.     * $table_name:表名   
  215.     * $condition:更新条件   
  216.     * $newdata:新的数据记录   
  217.     * $options:更新选择-upsert/multiple   
  218.     *   
  219.     * 返回值:   
  220.     * 成功:true   
  221.     * 失败:false   
  222.     */     
  223.     function update($table_name$condition$newdata$options=array())     
  224.     {     
  225.         $dbname = $this->curr_db_name;     
  226.         $options['safe'] = 1;     
  227.         if (!isset($options['multiple']))     
  228.         {     
  229.             $options['multiple'] = 0;          }     
  230.         try {     
  231.             $this->mongo->$dbname->$table_name->update($condition$newdata$options);     
  232.             return true;     
  233.         }     
  234.         catch (MongoCursorException $e)     
  235.         {     
  236.             $this->error = $e->getMessage();     
  237.             return false;     
  238.         }          }     
  239.      
  240.     /**   
  241.     * 删除记录   
  242.     *   
  243.     * 参数:   
  244.     * $table_name:表名   
  245.     * $condition:删除条件   
  246.     * $options:删除选择-justOne   
  247.     *   
  248.     * 返回值:   
  249.     * 成功:true   
  250.     * 失败:false   
  251.     */     
  252.     function remove($table_name$condition$options=array())     
  253.     {     
  254.         $dbname = $this->curr_db_name;     
  255.         $options['safe'] = 1;     
  256.         try {     
  257.             $this->mongo->$dbname->$table_name->remove($condition$options);     
  258.             return true;     
  259.         }     
  260.         catch (MongoCursorException $e)     
  261.         {     
  262.             $this->error = $e->getMessage();     
  263.             return false;     
  264.         }          }     
  265.      
  266.     /**   
  267.     * 查找记录   
  268.     *   
  269.     * 参数:   
  270.     * $table_name:表名   
  271.     * $query_condition:字段查找条件   
  272.     * $result_condition:查询结果限制条件-limit/sort等   
  273.     * $fields:获取字段   
  274.     *   
  275.     * 返回值:   
  276.     * 成功:记录集   
  277.     * 失败:false   
  278.     */     
  279.     function find($table_name$query_condition$result_condition=array(), $fields=array())     
  280.     {     
  281.         $dbname = $this->curr_db_name;     
  282.         $cursor = $this->mongo->$dbname->$table_name->find($query_condition$fields);     
  283.         if (!emptyempty($result_condition['start']))     
  284.         {     
  285.             $cursor->skip($result_condition['start']);     
  286.         }     
  287.         if (!emptyempty($result_condition['limit']))     
  288.         {     
  289.             $cursor->limit($result_condition['limit']);     
  290.         }     
  291.         if (!emptyempty($result_condition['sort']))     
  292.         {     
  293.             $cursor->sort($result_condition['sort']);     
  294.         }     
  295.         $result = array();     
  296.         try {     
  297.             while ($cursor->hasNext())     
  298.             {     
  299.                 $result[] = $cursor->getNext();     
  300.             }     
  301.         }     
  302.         catch (MongoConnectionException $e)     
  303.         {     
  304.             $this->error = $e->getMessage();     
  305.             return false;     
  306.         }     
  307.         catch (MongoCursorTimeoutException $e)     
  308.         {     
  309.             $this->error = $e->getMessage();     
  310.             return false;     
  311.         }     
  312.         return $result;     
  313.     }     
  314.      
  315.     /**   
  316.     * 查找一条记录   
  317.     *   
  318.     * 参数:   
  319.     * $table_name:表名   
  320.     * $condition:查找条件   
  321.     * $fields:获取字段   
  322.     *   
  323.     * 返回值:   
  324.     * 成功:一条记录   
  325.     * 失败:false   
  326.     */     
  327.     function findOne($table_name$condition$fields=array())     
  328.     {     
  329.         $dbname = $this->curr_db_name;     
  330.         return $this->mongo->$dbname->$table_name->findOne($condition$fields);     
  331.     }     
  332.      
  333.     /**   
  334.     * 获取当前错误信息   
  335.     *   
  336.     * 参数:无   
  337.     *   
  338.     * 返回值:当前错误信息   
  339.     */     
  340.     function getError()     
  341.     {     
  342.         return $this->error;     
  343.     }     
  344. }     
  345.      
  346. ?>  
posted @ 2013-06-07 16:42  hasayaki  阅读(835)  评论(0编辑  收藏  举报