PHP7操作MongoDB的增删改查和分页操作

原文博客地址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 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'];
$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

原创文章,作者:BVRJK,如若转载,请注明出处:https://www.wangzhanshi.com/n/6950.html

(0)
BVRJK的头像BVRJK
上一篇 2025年1月1日 16:38:00
下一篇 2025年1月1日 16:38:02

相关推荐

  • PHP7中如何实现协程

    什么是协程 先搞清楚,什么是协程。 你可能已经听过『进程』和『线程』这两个概念。 进程就是二进制可执行文件在计算机内存里的一个运行实例,就好比你的.exe文件是个类,进程就是new…

    php 2025年1月1日
  • PHP7中“??”运算符有什么作用

    PHP7中“??”运算符有什么作用很多人都不太了解,今天小编为了让大家更加了解“??”运算符,所以给大家总结了以下内容,一起往下看吧。 PHP7中 ?? 运算符,来看下具体作用 ?…

    2025年1月1日
  • PHP7下安装memcache和memcached扩展的方法

    memcache和memcached都是Memcached服务器的PHP扩展。其中memcache比memcached早出现,所以一些老的代码可能还在用memcache扩展。可以根…

    2025年1月1日
  • PHP5.2.x至PHP8.0.x版本升级新增特性

    PHP 8之所以具有出色的性能,是因为它引入了JIT编译器、优化了函数调用和数组操作、引入了新的数据结构和算法,并修复和优化了一些内部实现细节。这些改进使得PHP 8在执行速度和内…

    php 2024年12月17日
  • nginx服务器怎么从php5.5.7升级到php7

    ①、服务器nginx 、php 、mysql都是安装好的,于是想直接升级php7. ②按照文章:https://typecodes.com/web/centos7compileph…

    php 2025年1月1日
  • php7 fpm三种模式static、dynamic和ondemand的介绍

    我们经常使用php-fpm,但是不一定所有人都知道fpm有三种模式,今天小年就带大家了解一下fpm的三种模式,有需要的可以参考参考。 ; Choose how&…

    php 2025年1月1日
  • PHP实现基于文本的简易搜索引擎功能

    让这个功能可以在小型网站或者特定数据集内提供快速的关键字搜索能力,非常适合没有使用复杂数据库搜索引擎(如Elasticsearch)的场景。该搜索引擎将能够处理用户查询,扫描指定的…

    php 2024年12月17日
  • wamp2.5如何增加php7版本

    wamp2.5增加php7版本的方法:1、下载PHP7并将压缩包解压到wamp/bin/php目录下;2、下载VC15运行库安装;3、复制相关文件到php7.3.4文件夹中;4、修…

    2025年1月1日
  • vmware linux系统安装怎么php7

    php7比旧版本的性能提高了好多倍,linux通过yum安装php7。 首先我们先检查一下我们之前的php版本,通过yum list installed | grep php,如果…

    2025年1月1日
  • 如何解决php扩展安装不生效问题

    如何解决php扩展安装不生效问题?其实要解决这个问题也不难,这篇文章给出了相对应的分析和解答,下面我们一起来看看解决php扩展安装不生效问题的方法。 php安装扩展模块后,重启不生…

    php 2025年1月26日

发表回复

登录后才能评论