PHP7 Operation MongoDB Additions and deletions and paging operations

Source: Internet
Author: User
Tags mongodb


Original blog address www.xiegaosheng.com/post/view?id=96;

<?php/** * class mongodbclient * mongod Operation class  * you can change to singleton mode if you need to  */class  MongodbClient{      protected  $mongodb;    protected   $dbname;   protected  $collection;   protected  $bulk;    protected  $writeConcern;    public function __construct ($config)     {      if  (! $config [' dbname '] | |  ! $config [' collection '])  {         # code...          exit (' parameter error ');      }        $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: Query Method       * 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: Get Statistics      * 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: Paging Data       * 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: Update operation      * 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: inserting      * 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: Delete       * 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 ();   }    }//instantiation calls $action = $_get[' action ']?:exit (' parameter error '); $page  = $_get[' page ']?:1; $where  = json_decode ($_get[' where '],true)?: []; $limit  = $_get[' limit ']?: ' ten '; $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 ') {&NBSP;&NBsp;  $data  =  $mongodb->query ($where, $option);} ElseIf ($action = = ' page ') {    $data  =  $mongodb->page ($where, $page, $limit);} echo  $data, external call only  127.0.0.1/index.php?action= method &where= and so on parameters will return JSON


PHP7 Operation MongoDB Additions and deletions and paging operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.