佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > php操作mongodb封裝類與用法實(shí)例

php操作mongodb封裝類與用法實(shí)例

熱門標(biāo)簽:服務(wù)器配置 電子圍欄 阿里云 科大訊飛語音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 Linux服務(wù)器 團(tuán)購網(wǎng)站 銀行業(yè)務(wù)

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

近來學(xué)習(xí)了mongodb,剛好是做php開發(fā)的,隨便寫了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常規(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();
  }
  //返回錯(cuò)誤信息
  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ān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • PHP封裝的簡單連接MongoDB類示例
  • PHP使用mongoclient簡單操作mongodb數(shù)據(jù)庫示例
  • php如何利用pecl安裝mongodb擴(kuò)展詳解
  • TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作示例
  • PHP操作Mongodb封裝類完整實(shí)例
  • mongodb和php的用法詳解

標(biāo)簽:江蘇 大理 廣元 棗莊 蚌埠 衡水 萍鄉(xiāng) 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php操作mongodb封裝類與用法實(shí)例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    衡南县| 建湖县| 湘西| 新巴尔虎左旗| 临城县| 阿拉善左旗| 喀喇沁旗| 乳源| 罗山县| 余干县| 彩票| 汝城县| 和顺县| 广饶县| 曲麻莱县| 长乐市| 蕉岭县| 贵德县| 深州市| 旬阳县| 航空| 东丽区| 汽车| 南郑县| 乌鲁木齐市| 阿克| 北流市| 兰溪市| 锡林浩特市| 阿拉善左旗| 临洮县| 台山市| 永春县| 元阳县| 潜山县| 瓮安县| 交口县| 南平市| 桃江县| 封开县| 罗江县|