<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/7/3 0003 * Time: 下午 14:43 */ namespace mongo; use think\Exception; class Mongo { protected $mongodb; protected $dbname; protected $collection; protected $bulk; protected $writeConcern; /** * MongodbClient constructor. * @param $config */ public function __construct($config) { if (!$config['dbname'] || !$config['collection']) { exit('参数错误'); } $mongo_config = config('mongo_config'); $this->mongodb = new \MongoDB\Driver\Manager("mongodb://" . $mongo_config['mongodb_user'] . ":" . $mongo_config['mongodb_password'] . "@" . $mongo_config['mongodb_host'] . ":" . $mongo_config['mongodb_prot']); $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); } /** * 查询方法 * @param array $where * @param array $option * @return array * @throws \MongoDB\Driver\Exception\Exception */ public function query($where = [], $option = []) { try{ $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, $this->object_array($value)); } } return $data; }catch (\Throwable $e) { return $e->getMessage(); } } /** * @param $array * @return array */ function object_array($array) { if (is_object($array)) { $array = (array)$array; } if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = $this->object_array($value); } } return $array; } /** * @param array $where * @return int * @throws \MongoDB\Driver\Exception\Exception */ 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; } /** * 分页数据 * @param array $where * @param int $page * @param int $limit * @return mixed * @throws \MongoDB\Driver\Exception\Exception */ 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, 'sort' => ['systemReferNo' => -1], 'limit' => $limit ]; $data['data'] = $this->query($where, $options); $data['page'] = $endpage; return $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(); } }
php操作mongoDb
最新推荐文章于 2024-05-07 22:30:00 发布