- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class HMongodb {
-
-
- var $mongo;
-
- var $curr_db_name;
- var $curr_table_name;
- var $error;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function __construct($mongo_server, $connect=true, $auto_balance=true)
- {
- if (is_array($mongo_server))
- {
- $mongo_server_num = count($mongo_server);
- if ($mongo_server_num > 1 && $auto_balance)
- {
- $prior_server_num = rand(1, $mongo_server_num);
- $rand_keys = array_rand($mongo_server,$mongo_server_num);
- $mongo_server_str = $mongo_server[$prior_server_num-1];
- foreach ($rand_keys as $key)
- {
- if ($key != $prior_server_num - 1)
- {
- $mongo_server_str .= ',' . $mongo_server[$key];
- }
- }
- }
- else
- {
- $mongo_server_str = implode(',', $mongo_server);
- } }
- else
- {
- $mongo_server_str = $mongo_server;
- }
- try {
- $this->mongo = new Mongo($mongo_server, array('connect'=>$connect));
- }
- catch (MongoConnectionException $e)
- {
- $this->error = $e->getMessage();
- return false;
- }
- }
-
- function getInstance($mongo_server, $flag=array())
- {
- static $mongodb_arr;
- if (emptyempty($flag['tag']))
- {
- $flag['tag'] = 'default'; }
- if (isset($flag['force']) && $flag['force'] == true)
- {
- $mongo = new HMongodb($mongo_server);
- if (emptyempty($mongodb_arr[$flag['tag']]))
- {
- $mongodb_arr[$flag['tag']] = $mongo;
- }
- return $mongo;
- }
- else if (isset($mongodb_arr[$flag['tag']]) && is_resource($mongodb_arr[$flag['tag']]))
- {
- return $mongodb_arr[$flag['tag']];
- }
- else
- {
- $mongo = new HMongodb($mongo_server);
- $mongodb_arr[$flag['tag']] = $mongo;
- return $mongo; } }
-
-
-
-
-
-
-
-
-
-
- function connect()
- {
- try {
- $this->mongo->connect();
- return true;
- }
- catch (MongoConnectionException $e)
- {
- $this->error = $e->getMessage();
- return false;
- }
- }
-
-
-
-
-
-
-
-
- function selectDb($dbname)
- {
- $this->curr_db_name = $dbname;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- function ensureIndex($table_name, $index, $index_param=array())
- {
- $dbname = $this->curr_db_name;
- $index_param['safe'] = 1;
- try {
- $this->mongo->$dbname->$table_name->ensureIndex($index, $index_param);
- return true;
- }
- catch (MongoCursorException $e)
- {
- $this->error = $e->getMessage();
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- function insert($table_name, $record)
- {
- $dbname = $this->curr_db_name;
- try {
- $this->mongo->$dbname->$table_name->insert($record, array('safe'=>true));
- return true;
- }
- catch (MongoCursorException $e)
- {
- $this->error = $e->getMessage();
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
- function count($table_name)
- {
- $dbname = $this->curr_db_name;
- return $this->mongo->$dbname->$table_name->count();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function update($table_name, $condition, $newdata, $options=array())
- {
- $dbname = $this->curr_db_name;
- $options['safe'] = 1;
- if (!isset($options['multiple']))
- {
- $options['multiple'] = 0; }
- try {
- $this->mongo->$dbname->$table_name->update($condition, $newdata, $options);
- return true;
- }
- catch (MongoCursorException $e)
- {
- $this->error = $e->getMessage();
- return false;
- } }
-
-
-
-
-
-
-
-
-
-
-
-
-
- function remove($table_name, $condition, $options=array())
- {
- $dbname = $this->curr_db_name;
- $options['safe'] = 1;
- try {
- $this->mongo->$dbname->$table_name->remove($condition, $options);
- return true;
- }
- catch (MongoCursorException $e)
- {
- $this->error = $e->getMessage();
- return false;
- } }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function find($table_name, $query_condition, $result_condition=array(), $fields=array())
- {
- $dbname = $this->curr_db_name;
- $cursor = $this->mongo->$dbname->$table_name->find($query_condition, $fields);
- if (!emptyempty($result_condition['start']))
- {
- $cursor->skip($result_condition['start']);
- }
- if (!emptyempty($result_condition['limit']))
- {
- $cursor->limit($result_condition['limit']);
- }
- if (!emptyempty($result_condition['sort']))
- {
- $cursor->sort($result_condition['sort']);
- }
- $result = array();
- try {
- while ($cursor->hasNext())
- {
- $result[] = $cursor->getNext();
- }
- }
- catch (MongoConnectionException $e)
- {
- $this->error = $e->getMessage();
- return false;
- }
- catch (MongoCursorTimeoutException $e)
- {
- $this->error = $e->getMessage();
- return false;
- }
- return $result;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- function findOne($table_name, $condition, $fields=array())
- {
- $dbname = $this->curr_db_name;
- return $this->mongo->$dbname->$table_name->findOne($condition, $fields);
- }
-
-
-
-
-
-
-
-
- function getError()
- {
- return $this->error;
- }
- }
-
- ?>
数据库连接
-
- $m = new Mongo();
-
-
- $m = new Mongo("mongodb://${username}:${password}@localhost");
选择想要的collection:
- $mo = new Mongo();
- $coll = $mo->selectCollection('db', 'mycoll');
插入数据(MongoCollection对象)
- $coll = $mo->db->foo;
- $a = array('a' => 'b');
- $options = array('safe' => true);
- $rs = $coll->insert($a, $options);
更新数据库中的记录(MongoCollection对象)
- $coll = $mo->db->coll;
- $c = array('a' => 1, 's' => array('$lt' => 100));
- $newobj = array('e' => 'f', 'x' => 'y');
- $options = array('safe' => true, 'multiple' => true);
- $rs = $coll->update($c, $newobj, $options);
删除数据库中的记录(MongoCollection对象)
- $coll = $mo->db->coll;
- $c = array('a' => 1, 's' => array('$lt' => 100));
- $options = array('safe' => true);
- $rs = $coll->remove($c, $options);
查询collection获得多条记录(MongoCollection类)
- $coll = $mo->db->coll;
- $query = array('s' => array('$lt' => 100));
- $fields = array('a' => true, 'b' => true);
- $rs = $coll->find($query, $fields);
查询collection获得单条记录(MongoCollection类)
- $coll = $mo->db->coll;
- $query = array('s' => array('$lt' => 100));
- $fields = array('a' => true, 'b' => true);
- $rs = $coll->findOne($query, $fields);
针对游标对象MongoCursor的操作(MongoCursor类):
- $cursor = $coll->find($query,$fields);
- while($cursor->hasNext()){
- $r = $cursor->getNext();
- var_dump($r);
- }
-
- $cursor = $coll->find($query,$fields);
- foreache($cursor as $k=>$v){
- var_dump($v);
- }
-
- $cursor = $coll->find($query,$fields);
- $array= iterator_to_array($cursor);
删除一个数据库
删除当前DB
- $db = $mo->dbname;
- $db->drop();
列出所有可用数据库
获得当前数据库名
- $db = $mo->dbname;
- $db->_tostring();