PHP7操作MongoDB的增刪改查和分頁操作

來源:互聯網
上載者:User

標籤:mongodb   mongodb分頁   php7   


原文部落格地址www.xiegaosheng.com/post/view?id=96;

<?php/** * Class MongodbClient * mongod操作類 *如果需要自己也可以改成單例模式 */class MongodbClient{      protected $mongodb;   protected $dbname;   protected $collection;   protected $bulk;   protected $writeConcern;   public function __construct($config)   {      if (!$config['dbname'] || !$config['collection']) {         # code...         exit('參數錯誤');      }      $this->mongodb = new MongoDB\Driver\Manager("mongodb://localhost:27017");      $this->dbname = $config['dbname'];      $this->collection = $config['collection'];      $this->bulk = new MongoDB\Driver\BulkWrite();      $this->writeConcern   = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);   }    /**     * Created by PhpStorm.     * function: query     * Description:查詢方法     * User: Xiaoxie     * Email [email protected]     * @param array $where     * @param array $option     * @return string     *     */   public function query($where=[],$option=[])   {      $query = new MongoDB\Driver\Query($where,$option);      $result = $this->mongodb->executeQuery("$this->dbname.$this->collection", $query);      $data = [];      if ($result) {         # code...         foreach ($result as $key => $value) {            # code...            array_push($data, $value);         }      }      return json_encode($data);   }    /**     * Created by PhpStorm.     * function: getCount     * Description:擷取統計數     * User: Xiaoxie     * Email [email protected]     * @param array $where     * @return int     *     */   public function getCount($where=[])   {      $command = new MongoDB\Driver\Command(['count' => $this->collection,'query'=>$where]);      $result = $this->mongodb->executeCommand($this->dbname,$command);      $res = $result->toArray();      $cnt = 0;      if ($res) {         # code...         $cnt = $res[0]->n;      }      return $cnt;   }    /**     * Created by PhpStorm.     * function: page     * Description:分頁資料     * User: Xiaoxie     * Email [email protected]     * @param array $where     * @param int $page     * @param int $limit     * @return string     *     */   public function page($where=[],$page=1,$limit=10)   {            $count = $this->getCount($where);      $data['count'] = $count;      $endpage = ceil($count/$limit);      if ($page>$endpage) {         # code...         $page = $endpage;      }elseif ($page <1) {         $page = 1;      }      $skip = ($page-1)*$limit;      $options = [         'skip'=>$skip,          'limit'    => $limit      ];      $data['data'] = $this->query($where,$options);      $data['page'] = $endpage;      return json_encode($data);   }    /**     * Created by PhpStorm.     * function: update     * Description:更新操作     * User: Xiaoxie     * Email [email protected]     * @param array $where     * @param array $update     * @param bool $upsert     * @return int|null     *     */   public function update($where=[],$update=[],$upsert=false)   {      $this->bulk->update($where,['$set' => $update], ['multi' => true, 'upsert' => $upsert]);      $result = $this->mongodb->executeBulkWrite("$this->dbname.$this->collection", $this->bulk, $this->writeConcern);      return $result->getModifiedCount();   }    /**     * Created by PhpStorm.     * function: insert     * Description:插入     * User: Xiaoxie     * Email [email protected]     * @param array $data     * @return mixed     *     */   public function insert($data=[])   {      $result = $this->bulk->insert($data);      return $result->getInsertedCount();   }    /**     * Created by PhpStorm.     * function: delete     * Description:刪除     * User: Xiaoxie     * Email [email protected]     * @param array $where     * @param int $limit     * @return mixed     *     */   public function delete($where=[],$limit=1)   {      $result = $this->bulk->delete($where,['limit'=>$limit]);      return $result->getDeletedCount();   }   }//執行個體化調用$action = $_GET['action']?:exit('參數錯誤');$page = $_GET['page']?:1;$where = json_decode($_GET['where'],true)?:[];$limit = $_GET['limit']?:'10';$data = json_decode($_GET['data'],true)?:[];$option = json_decode($_GET['option'],true)?:[];$collection = $_GET['collection'];$mongodb = new MongodbClient(['dbname'=>$dbname,'collection'=>$collection]);if ($action=='getCount') {   # code...   $data = $mongodb->getCount($where);}elseif($action=='insert'){   $data = $mongodb->insert($data);}elseif($action=='update'){   $data = $mongodb->update($where,$data);}elseif($action=='delete'){   $data = $mongodb->delete($where);}elseif($action=='query'){   $data = $mongodb->query($where,$option);}elseif($action=='page'){   $data = $mongodb->page($where,$page,$limit);}echo $data;外部調用的時候只需 127.0.0.1/index.php?action=方法&where=等等參數就會返回json


PHP7操作MongoDB的增刪改查和分頁操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.