国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php操作mongodb封裝類與用法實例

2024-05-05 00:05:12
字體:
來源:轉載
供稿:網友

本文實例講述了php操作mongodb封裝類與用法。分享給大家供大家參考,具體如下:

近來學習了mongodb,剛好是做php開發的,隨便寫了php操作mongodb的封裝類.

<?php/** * Created by PhpStorm. * User: lee * Date: 2016/10/24 * Time: 13:49 */namespace App/Http/Controllers/Api;use App/Http/Common/ReturnApi;/* * * mongdb常規操作 */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');  }//禁止克隆  //創建索引  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);  }  //返回表的記錄數  public function count($table)  {    return self::$mon->$table->count();  }  //返回錯誤信息  public function getError()  {    return self::$error;  }}

操作實例:

use App/Http/Controllers/Api/MongdbCommonController;$db = MongdbCommonController::getInstance();$collection = 'tab';$data = array('tt' =>'sdsd', 'pp'  => 'ssdsdf');//返回記錄數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'));

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 砚山县| 张掖市| 从化市| 西贡区| 延安市| 黔西县| 平度市| 富源县| 阿克苏市| 海阳市| 三门峡市| 河源市| 乌鲁木齐县| 青铜峡市| 磐石市| 承德市| 澄城县| 湘乡市| 吉水县| 临桂县| 册亨县| 托克托县| 图木舒克市| 天气| 缙云县| 水城县| 土默特右旗| 甘肃省| 资源县| 盘山县| 伽师县| 清水县| 绍兴市| 五寨县| 延川县| 手游| 台南县| 宜良县| 同仁县| 积石山| 腾冲县|