网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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


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

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名虚拟主机、营销软件、网站建设、海晏网站维护、网站推广。

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

标题名称:PHP7操作MongoDB的增删改查和分页操作
URL地址:http://bjjierui.cn/article/ispoid.html

其他资讯