PHP7操作MongoDB的增删改查和分页操作
admin
2023-06-13 17:42:26
0


原文博客地址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


相关内容

热门资讯

重庆永川遭遇特大暴雨,67岁会... 5月26日上午11点多,潇湘晨报·晨视频记者从重庆市委宣传部获悉,在重庆永川区茶山竹海街道大桥村特大...
陈克明为“手擀”商标道歉,40... 极目新闻评论员 屈旌一袋挂面能藏着多少的心眼?陈克明以40年商誉为代价,给出了一个令人唏嘘的答案。近...
荆州鸿创机械取得折弯机用挡料机... 国家知识产权局信息显示,荆州鸿创机械制造有限公司取得一项名为“一种折弯机用挡料机构”的专利,授权公告...
领雁科技数智渠道5.0:助力银... 5月23日,信华信集团举办三十周年庆典,同期召开“智敬未来·深度经营——领雁AI+金融科技创新产品发...
视频丨香港各界:神舟二十三号成... 神舟二十三号载人飞船发射取得圆满成功,引发香港社会各界热烈反响。在接受总台记者采访时,不少人表示深受...
算法1年翻倍,芯片2年翻倍?重... 新智元报道 【新智元导读】NBER重磅论文证明:AI研发的自我加速反馈环强度远超所有科技领域,算...
伊朗外交部发言人否认收取海峡“... 【文/观察者网 齐倩】美媒近期频繁放风称,美伊即将达成协议。但伊朗方面表示,双方仍就一、两项条款存在...
“永川兄弟妯娌4人被洪水卷走”... 极目新闻记者 谢茂 邓波5月23日夜间至24日凌晨,重庆市永川区突发瞬时极端特大暴雨引发山洪和地灾,...
朱媛媛,提名最佳女配角 5月26日上午,第31届上海电视节白玉兰奖电视剧类别中国电视剧单元入围名单揭晓。其中,朱媛媛凭借遗作...
“迄今为止最严厉表态”!加总理... 【环球网报道】据英国广播公司(BBC)、美国《纽约时报》等媒体报道,加拿大总理卡尼当地时间25日表示...