分享一个PHP实现的MongoDB数据库操作类,参考mysql版的操作类实现的。
完整代码:

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