原文博客地址www.xiegaosheng.com/post/view?id=96;

<?php/***ClassMongodbClient*mongod操作类*如果需要自己也可以改成单例模式*/classMongodbClient{protected$mongodb;protected$dbname;protected$collection;protected$bulk;protected$writeConcern;publicfunction__construct($config){if(!$config['dbname']||!$config['collection']){#code...exit('参数错误');}$this->mongodb=newMongoDB\Driver\Manager("mongodb://localhost:27017");$this->dbname=$config['dbname'];$this->collection=$config['collection'];$this->bulk=newMongoDB\Driver\BulkWrite();$this->writeConcern=newMongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,100);}/***CreatedbyPhpStorm.*function:query*Description:查询方法*User:Xiaoxie*Email736214763@qq.com*@paramarray$where*@paramarray$option*@returnstring**/publicfunctionquery($where=[],$option=[]){$query=newMongoDB\Driver\Query($where,$option);$result=$this->mongodb->executeQuery("$this->dbname.$this->collection",$query);$data=[];if($result){#code...foreach($resultas$key=>$value){#code...array_push($data,$value);}}returnjson_encode($data);}/***CreatedbyPhpStorm.*function:getCount*Description:获取统计数*User:Xiaoxie*Email736214763@qq.com*@paramarray$where*@returnint**/publicfunctiongetCount($where=[]){$command=newMongoDB\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;}/***CreatedbyPhpStorm.*function:page*Description:分页数据*User:Xiaoxie*Email736214763@qq.com*@paramarray$where*@paramint$page*@paramint$limit*@returnstring**/publicfunctionpage($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;returnjson_encode($data);}/***CreatedbyPhpStorm.*function:update*Description:更新操作*User:Xiaoxie*Email736214763@qq.com*@paramarray$where*@paramarray$update*@parambool$upsert*@returnint|null**/publicfunctionupdate($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();}/***CreatedbyPhpStorm.*function:insert*Description:插入*User:Xiaoxie*Email736214763@qq.com*@paramarray$data*@returnmixed**/publicfunctioninsert($data=[]){$result=$this->bulk->insert($data);return$result->getInsertedCount();}/***CreatedbyPhpStorm.*function:delete*Description:删除*User:Xiaoxie*Email736214763@qq.com*@paramarray$where*@paramint$limit*@returnmixed**/publicfunctiondelete($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=newMongodbClient(['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