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