<?php
class MongoDB{
private $_manager;
private $_host;
private $_username;
private $_password;
private $_db;
public function __construct($param = array()){
if(count($param)){
$this->_host = $param['host'];
$this->_username = $param['username'];
$this->_password = $param['password'];
$this->_db = $param['db'];
$mongo = "mongodb://" . $this->_username . ':' . $this->_password . '@' . $this->_host . '/';
}else{
return false;
}
$this->_manager = new \MongoDB\Driver\Manager($mongo);
}
public function getInstense(){
return $this->_manager;
}
public function getDB(){
return $this->_db;
}
public function getBulk(){
return new \MongoDB\Driver\BulkWrite;
}
public function getWriteConcern(){
new \MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
}
/**
* 插入數據
* @param $db 數據庫名
* @param $collection 集合名
* @param $document 數據 json格式
* @return
*/
public function insert($collection, $document){
$bulk = $this->getBulk();
$write_concern = $this->getWriteConcern();
$document = json_decode($document);
if( count($document) == 1 ){
$document['_id'] = new \MongoDB\BSON\ObjectID;
$bulk->insert($document);
}else{
foreach ($document as $val){
$val['_id'] = new \MongoDB\BSON\ObjectID;
$bulk->insert($val);
}
}
$this->_manager->executeBulkWrite($this->_db.$collection, $bulk);
return $this->_manager->executeBulkWrite($this->_db. '.' .$collection, $bulk, $write_concern);
}
/**
* 刪除數據
* @param array $where
* @param array $option
* @param string $db
* @param string $collection
* @return mixed
*/
public function delete($collection, $where = array(), $option = array()){
$bulk = $this->getBulk();
$bulk->delete($where, $option);
return $this->_manager->executeBulkWrite($this->_db. '.' .$collection, $bulk);
}
/**
* 更新數據
* @param array $where 類似where條件
* @param array $field 要更新的字段
* @param bool $upsert 如果不存在是否插入,默認爲false不插入
* @param bool $multi 是否更新全量,默認爲false
* @param string $db 數據庫
* @param string $collection 集合
* @return mixed
*/
public function update($collection, $where = array(), $field = array(), $upsert = false, $multi = false){
$bulk = $this->getBulk();
$write_concern = $this->getWriteConcern();
$bulk->update($where, $field, $upsert, $multi);
return $this->_manager->executeBulkWrite($this->_db. '.' .$collection, $bulk, $write_concern);
}
public function selectById($collection, $id, $options = array()){
$filter = ['_id' => new \MongoDB\BSON\ObjectID($id)];
$res = $this->query($collection, $filter, $options);
foreach ($res as $item) {
$data = $this->objToArray($item);
}
return $data;
}
public function query($collection, $filter, $options){
$query = new \MongoDB\Driver\Query($filter, $options);
$res = $this->_manager->executeQuery($this->_db. '.' .$collection, $query);
$data = array();
foreach ($res as $item){
$tmp = $this->objToArray($item);
$tmp['_id'] = $tmp['_id']['$oid'];
$data[] = $tmp;
}
return $data;
}
/**
* 執行MongoDB命令
* @param array $param
* @return \MongoDB\Driver\Cursor
*/
public function command(array $param)
{
$cmd = new \MongoDB\Driver\Command($param);
return $this->_manager->executeCommand($this->_db, $cmd);
}
/**
* 按條件計算個數
*
* @param string $collName 集合名
* @param array $where 條件
* @return int
*/
public function count($collName, array $where)
{
$result = 0;
$cmd = [
'count' => $collName,
'query' => $where
];
$arr = $this->command($cmd)->toArray();
if (!empty($arr)) {
$result = $arr[0]->n;
}
return $result;
}
/**
* 聚合查詢
* @param $collName
* @param array $where
* @param array $group
* @return \MongoDB\Driver\Cursor
*/
function aggregate($collName, array $where, array $group)
{
$cmd = [
'aggregate' => $collName,
'pipeline' => [
['$match' => $where],
['$group' => $group]
]
];
//print_r($cmd);exit();
$result = $this->command($cmd)->toArray();
// print_r($result);exit();
return $result[0]->result;
}
/**
* 同mysql中的distinct功能
*
* @param string $collName collection名
* @param string $key 要進行distinct的字段名
* @param array $where 條件
* @return array
* Array
* (
* [0] => 1.0
* [1] => 1.1
* )
*/
function distinct($collName, $key, array $where)
{
$result = [];
$cmd = [
'distinct' => $collName,
'key' => $key,
'query' => $where
];
$arr = $this->command($cmd)->toArray();
if (!empty($arr)) {
$result = $arr[0]->values;
}
return $result;
}
public function objToArray($data){
return json_decode(json_encode($data),true);
}
}
PHP封裝MongoDB類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.