首頁 後端開發 php教程 mongodb php 操作類別 帶幾個簡單的例子

mongodb php 操作類別 帶幾個簡單的例子

Jul 29, 2016 am 08:51 AM
gt mongo nbsp quot server

類別 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教程有興趣的朋友有所幫助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

解決方法:您的組織要求您更改 PIN 碼 解決方法:您的組織要求您更改 PIN 碼 Oct 04, 2023 pm 05:45 PM

解決方法:您的組織要求您更改 PIN 碼

Windows 11 上調整視窗邊框設定的方法:變更顏色和大小 Windows 11 上調整視窗邊框設定的方法:變更顏色和大小 Sep 22, 2023 am 11:37 AM

Windows 11 上調整視窗邊框設定的方法:變更顏色和大小

如何在 Windows 11 上變更標題列顏色? 如何在 Windows 11 上變更標題列顏色? Sep 14, 2023 pm 03:33 PM

如何在 Windows 11 上變更標題列顏色?

OOBELANGUAGE錯誤Windows 11 / 10修復中出現問題的問題 OOBELANGUAGE錯誤Windows 11 / 10修復中出現問題的問題 Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE錯誤Windows 11 / 10修復中出現問題的問題

Windows 11 上啟用或停用工作列縮圖預覽的方法 Windows 11 上啟用或停用工作列縮圖預覽的方法 Sep 15, 2023 pm 03:57 PM

Windows 11 上啟用或停用工作列縮圖預覽的方法

華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

華為GT3 Pro和GT4的差異是什麼?

Windows 11 上的顯示縮放比例調整指南 Windows 11 上的顯示縮放比例調整指南 Sep 19, 2023 pm 06:45 PM

Windows 11 上的顯示縮放比例調整指南

10種在 Windows 11 上調整亮度的方法 10種在 Windows 11 上調整亮度的方法 Dec 18, 2023 pm 02:21 PM

10種在 Windows 11 上調整亮度的方法

See all articles