1. 程式人生 > >分享下mongodb封裝的幾個方法

分享下mongodb封裝的幾個方法

<?php /** * Created by sublime. * User: XXXX * Date: 2017/12/1 * Time: 15:44 */ namespace Lib; use MongoClient; //下面是驅動類,現在沒有用到,我的版本php5.5,如果是php7就要用到這些類 use MongoDB\BSON\ObjectID; use MongoDB\Driver\BulkWrite; use MongoDB\Driver\Command; use MongoDB\Driver\Cursor; use MongoDB\Driver\Exception\AuthenticationException
; use MongoDB\Driver\Exception\BulkWriteException; use MongoDB\Driver\Exception\ConnectionException; use MongoDB\Driver\Exception\InvalidArgumentException; use MongoDB\Driver\Exception\RuntimeException; use MongoDB\Driver\Manager; use MongoDB\Driver\Query as MongoQuery; use MongoDB\Driver\ReadPreference
; use MongoDB\Driver\WriteConcern; /** * PHP操作mongodb資料庫操作類 */ class MongoDb { protected $database = ''; protected $mo; /** * 構造方法 */ public function __construct() { $database = DBNAME; $mongo = $this->getConnect(DBSERVER, DBUSER, DBPASS, DBPORT); $this
->database = $mongo->$database; } /** * 資料庫單例方法 * @param $server * @param $user * @param $password * @param $port * test 測試 * @return Mongo */ /*public function getConnect($server, $user, $password, $port) { // echo "mongodb://{$server}:{$port}";die; // $mongo = new MongoClient("mongodb://{$server}:{$port}"); $mongo = new MongoClient("mongodb://{$server}:{$port}"); return $mongo; }*/ /** * 資料庫單例方法 * @param $server * @param $user * @param $password * @param $port * @return Mongo */ public function getConnect($server, $user, $password, $port) { if (isset($this->mo)) { return $this->mo; } else { if (!empty($server)) { if (!empty($port)) { if (!empty($user) && !empty($password)) { $this->mo = new MongoClient("mongodb://{$user}:{$password}@{$server}:{$port}"); } else { $this->mo = new MongoClient("mongodb://{$server}:{$port}"); } } else { $this->mo = new MongoClient("mongodb://{$server}"); } } else { $this->mo = new MongoClient(); } return $this->mo; } } /** * 查詢表中所有資料 * @param $table * @param array $where * @param array $sort * @param string $limit * @param string $skip * @return array|int */ public function getAll($table, $where = array(), $sort = array(), $limit = '', $skip = '') { if (!empty($where)) { $data = $this->database->$table->find($where); } else { $data = $this->database->$table->find(); } if (!empty($sort)) { $data = $data->sort($sort); } if (!empty($limit)) { $data = $data->limit($limit); } if (!empty($skip)) { $data = $data->skip($skip); } $newData = array(); while ($data->hasNext()) { $newData[] = $data->getNext(); } if (count($newData) == 0) { return 0; } return $newData; } /** * 查詢表中所有資料,將_id 物件變成陣列 * @param $table * @param array $where * @param array $sort * @param string $limit * @param string $skip * @return array */ public function getAllArray($table, $where = array(), $sort = array(), $limit = '', $skip = '') { if (!empty($where)) { $data = $this->database->$table->find($where); } else { $data = $this->database->$table->find(); } if (!empty($sort)) { $data = $data->sort($sort); } if (!empty($limit)) { $data = $data->limit($limit); } if (!empty($skip)) { $data = $data->skip($skip); } $newData = array(); while ($data->hasNext()) { $newData[] = $data->getNext(); } if (count($newData) == 0) { return 0; } foreach ($newData as $key => $val) { $id = $val['_id']->{'$id'}; $newData[$key]['_id'] = $id; } return $newData; } /** * 查詢指定一條資料 * @param $table * @param array $where * @return int */ public function getOne($table, $where = array()) { if (!empty($where)) { $data = $this->database->$table->findOne($where); } else { $data = $this->database->$table->findOne(); } return $data; } /** * 統計個數 * @param $table * @param array $where * @return mixed */ public function getCount($table, $where = array()) { if (!empty($where)) { $data = $this->database->$table->find($where)->count(); } else { $data = $this->database->$table->find()->count(); } return $data; } /** * 直接執行mongo命令 * @param $sql * @return array */ public function toExcute($sql) { $result = $this->database->execute($sql); return $result; } /** * 分組統計個數 * @param $table * @param $where * @param $field */ public function groupCount($table, $where, $field) { $cond = array( array( '$match' => $where, ), array( '$group' => array( '_id' => '$' . $field, 'count' => array('$sum' => 1), ), ), array( '$sort' => array("count" => -1), ), ); $this->database->$table->aggregate($cond); } /** * 刪除資料 * @param $table * @param $where * @return array|bool */ public function toDelete($table, $where) { $re = $this->database->$table->remove($where); return $re; } /** * 插入資料 * @param $table * @param $data * @return array|bool */ public function toInsert($table, $data) { $re = $this->database->$table->insert($data); return $re; } /** * 更新資料 * @param $table * @param $where * @param $data * @return bool */ public function toUpdate($table, $where, $data) { $re = $this->database->$table->update($where, array('$set' => $data)); return $re; } /** * 獲取唯一資料 * @param $table * @param $key * @return array */ public function distinctData($table, $key, $query = array()) { if (!empty($query)) { $where = array('distinct' => $table, 'key' => $key, 'query' => $query); } else { $where = array('distinct' => $table, 'key' => $key); } $data = $this->database->command($where); return $data['values']; } } ?>