php操作mongodb
<?php /** * php操作mongodb * 127.0.0.1/index.php?action=方法&where=等等参数就会返回json */ 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 736214763@qq.com * @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 736214763@qq.com * @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 736214763@qq.com * @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 736214763@qq.com * @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 736214763@qq.com * @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 736214763@qq.com * @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'];//库名.集合名 $dbname = 'local'; $mongodb = new MongodbClient(['dbname' => $dbname, 'collection' => $collection]); if ($action == 'getCount') { $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;
赞:(0)
踩:(0)
- 相关文章
- php7中使用mongodb的aggregate进行
- php7 mongodb distinct及count方法
- mongodb基础知识
- mongodb报No suitable servers fou
- mongodb学习网址及基础教程
- mongodb数据库添加及删除
- mongodb集合创建与删除
- mongodb文档添加与更新
- mongodb文档查询
- php7 mongodb操作类
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- laravel页面静态化的方法
- centos7.4 64位下swoole安装及配置
- navicate连接mycat报1184错误
- curl设置超时不起作用(CURLOPT_TIM
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:623篇
- 浏览数:1303109