您好,登錄后才能下訂單哦!
本文章向大家介紹怎么在php中操作mongodb封裝類的基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價值,需要的朋友可以參考一下。
php是一個嵌套的縮寫名稱,指的是英文超級文本預(yù)處理語言(php:Hypertext Preprocessor)的縮寫,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
mongodb的封裝類.
<?php namespace App\Http\Controllers\Api; use App\Http\Common\ReturnApi; /* * * mongdb常規(guī)操作 */ class MongdbCommonController { private static $conn; private static $mon; private static $error; private function __construct(){ //self::$conn = new \MongoClient("mongodb://".env('MONGDB_USER').":".env('MONGDB_PASS')."@".env('MONGDB_HOST').":".env('MONGDB_PORT')."/".env('MONGDB_DB')); self::$conn = new \MongoClient("mongodb://".env('MONGDB_USER').":".env('MONGDB_PASS')."@".env('MONGDB_HOST').":".env('MONGDB_PORT')); //self::$conn = new \MongoClient("mongodb://".env('MONGDB_HOST').":".env('MONGDB_PORT')); $db = env('MONGDB_DB'); self::$mon = self::$conn->$db; } public static function getInstance(){ if(!(self::$conn instanceof self)){ self::$conn = new self(); } //return self::$conn->mydb; return self::$conn; } private function __clone(){ trigger_error('Clone is not allowed'); }//禁止克隆 //創(chuàng)建索引 public function ensureIndex($table, $index, $index_param=array()) { $index_param['safe'] = 1; try { self::$mon->$table->ensureIndex($index, $index_param); return true; } catch (MongoCursorException $e) { self::$error = $e->getMessage(); return false; } } //添加 public function insert($table,$arr){ try { self::$mon->$table->insert($arr, array('w'=>true)); return true; } catch (MongoCursorException $e) { self::$error = $e->getMessage(); return false; } } //更新 public function update($table, $condition, $new_arr, $options=array()) { $options['w'] = 1; if (!isset($options['multiple'])) { $options['multiple'] = 0; } try { self::$mon->$table->update($condition, $new_arr, $options); return true; } catch (MongoCursorException $e) { self::$error = $e->getMessage(); return false; } } //刪除 public function remove($table, $condition, $options=array()) { $options['w'] = 1; try { self::$mon->$table->remove($condition, $options); return true; } catch (MongoCursorException $e) { self::$error = $e->getMessage(); return false; } } //查找 public function find($table, $query_condition, $result_condition=array(), $fields=array()) { $cursor = self::$mon->$table->find($query_condition, $fields); if (!empty($result_condition['start'])) { $cursor->skip($result_condition['start']); } if (!empty($result_condition['limit'])) { $cursor->limit($result_condition['limit']); } if (!empty($result_condition['sort'])) { $cursor->sort($result_condition['sort']); } $result = array(); try { while ($cursor->hasNext()) { $result[] = $cursor->getNext(); } } catch (MongoCursorTimeoutException $e) { self::$error = $e->getMessage(); return false; } return $result; } //查找一條記錄 public function findOne($table, $condition, $fields=array()) { return self::$mon->$table->findOne($condition, $fields); } //返回表的記錄數(shù) public function count($table) { return self::$mon->$table->count(); } //返回錯誤信息 public function getError() { return self::$error; } }
操作實(shí)例:
use App\Http\Controllers\Api\MongdbCommonController; $db = MongdbCommonController::getInstance(); $collection = 'tab'; $data = array('tt' =>'sdsd', 'pp' => 'ssdsdf'); //返回記錄數(shù) echo $db->count($collection); //插入記錄 $db->insert($collection, array("id"=>2, "title"=>"asdqw")); //更新 $db->update($collection, array("id"=>2),array('tt'=>'dfdfd',"gg"=>"bbb",'hh'=>'dfsdsd')); //查找記錄 echo '<pre>'; print_r( $db->find($collection, array("tt"=>'dfdfd'), array("start"=>1,"limit"=>4))); //刪除 $db->remove($collection, array('tt' =>'sdsd'));
以上就是小編為大家?guī)淼脑趺丛趐hp中操作mongodb封裝類的全部內(nèi)容了,希望大家多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。