類別 NewMongodb {
橡樹 $mongo; //NewMongodb連線
private $curr_db_name;
private $curr_table_name;
private $error;
public $config; {
static $NewMongodb_arr;
若(空($flag['tag']))
{
$flag['tag'] = '預設'; }
if (isset($flag['force']) && $flag['force'] == true)
{
if (empty($NewMongodb_arr[$flag[ 'tag']]))
{
$NewMongodb_arr[$flag['tag'] $mongo;
}
else if (isset($NewMongodb_arr[$flag['tag'] ]) && is_resource($NewMongodb_arr[$flag['tag']])
{
$mongo = new NewMongodb($mongo_server) ;
$NewMongodb_arr[$flag['tag']] = $mongo;
return $mongo; public function __construct($mongo_server, $c $auto_balance=正確)
{
if (is_array($mongo_server))
{
$mongo_server_num = count($mongo_server);); {
$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]; if ($key != $prior_server_數字- 1 )
{
$mongo_server_str .= ',' . $mongo_server[$key];
}
}
}
} }
else
{
$this->mongo = new MongoClient($mongo_server, array('connect'=>$connect));
}
error = $e-> ;getMessage( );
返回false;
}
}
/**
* 建構子
* 支援傳入多個mongo_server(1.一個問題時連接其它的server 2.自動將查詢均勻分送至不同server)
*
: 。 ,預設是
*
* 回傳值:
* 成功:mongo object
* 失敗:false
公 $this->mongo-> ;連結();
回復正確;
}
catch (MongoConnectionException $e)
{
$this->error = $ }
}
/**
* select db
*
* 參數:$dbname
*
* 傳回值:無
*/
public function selectDb($dbname)
{
$this->curr_db_name = $dbname;
}
/**
* 建立索引:如索引已存在,則傳回。
*
* 參數:
* $table_name:表名
* $index:索引-array("id"=>1)-在id欄位建立昇序索引
* 回傳值:
* 成功:true
* 失敗:false
*/
public function ensure ($table_name, $
public function ensure name = $this->curr_db_name;
$ index_param['safe'] = 1;
try {
$this->mongo->$dbname->$table_name->ensureIndex($index, $index_param) }
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
return false;
return false; &*/
public function insert($table_name, $record)
{
$dbname = $this->curr_db_name;
try {
$this->mongo->$dbname->$table_name->insert($record, array('safe'= re); }
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
/**
* 插入記錄
*
* 參數:
* $table_name:表名
* $record:
*/
public function count($table_name)
{
$dbname = $this->curr_db_name;
return $this->mongo->$dbname->$table_name->count();
}
condition, $newdata, $opti> {
$dbname = $this->curr_db_name;
$options['safe'] = 1;
$options['safe'] = 1;
) {
$options ['multiple'] = 0; }
try {
$this->mongo->$dbname->$table_name-pate($table_name-pate,$ return true;
}
catch (MongoCursorException $e)
{
$this->error = $e->getMessage();
return false;
return false;
/**
* 刪除記錄
*
* 參數:
* $table_name:表名
* $condition: * 成功:true
* 失敗:false
*/
public function remove($table_name, $condition, $opti> {
$dbname = $this->curr_db_name;name; 嘗試 {
$ this->mongo->$dbname->$table_name->remove($condition, $options);
return true;
}🎠
$this-> ;錯誤= $e-> getMessage();
return false;
} }
/**
* 尋找記錄
*
* 參數:
* $table_name:表名
* $query_condition: *
*回傳值:
* 成功:記錄集
* 失敗:false
*/
) $ public function find($; {
$ dbname = $this-> curr_db_name;
$cursor = $this->mongo->$dbname->$table_name->find($query_condition, $fields);
if (!empty($result_dition $cursor ->skip($result_condition['start']);
}
if (!empty($result_condition['limit'])) 'limit']);
}
if (!empty($result_condition['sort']))
{
$cursor->sort($result_yort($res); ();
嘗試{
同時 ( $cursor->hasNext())
{
$result[] = $cursor-> catch (MongoConnectionException $e)
{
$this->error = $e->getMessage( );
return false;
}
catch (MongoCursorTime >getMessage() ;
return false;
}
回傳$ #&*/
public function findOne($table_name, $condition, $fields=array())
{
$ ->findOne($condition, $fields);
}
/**
* 尋找一筆記錄
*
* 參數:
* $table_name:表名
* $condition :一筆記錄
* 失敗:false
*/
public function getError( ) 1
/*** NewMongodb類別** examples:
* $mongo = new NewMongodb("127.0.0.1:11223");
* $mon selectDb("test_db");
* $mon test_table", array("id"=>1), array('unique'=>true));
* 取得表格的記錄
* $mongo->count("test_table");
* 插入記錄mongo->insert("test_table", array("id"=>2, "title"=>"asdqw"));
* 更新記錄
* $mongo->update("test_table", array("id" =>1),array("id"=>1,"title"=>"bbb"));
* 更新記錄-存在時更新,不存在時添加-相當於set
* $mongo->update( "test_table", array("id"=>1),array("id"=>1,"title"=>"bbb"),array("upsert"=>1));
* 尋找記錄
* $mongo->find("c", array("title"=>"asdqw"), array("start"=>2,"limit"=>2,"sort"=>array("id"=> 1)))
* 找一筆記錄
* $mongo->findOne("$mongo->findOne("ttt", array("id"=>1))", array("id"=>1)) ;
* 刪除記錄
* $mongo->remove("ttt", array("title"=>"bbb"));
* 只刪除一筆記錄
* $ "title"=>"bbb"), array("justOne"=>1));
* 取得Mongo操作的錯誤訊息
* $mongo->getError();
*/
}
以上就介紹了mongodb php 操作類別 帶幾個簡單的例子,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。