這是我自己封裝的,隔一段時間再回來看這些代碼時,我有點蒙圈了,原來我可以這麼牛,我都不相信這是我自己寫的了
<?php
namespace app\admin\model;
use think\Model;
use MongoDB\Driver\Manager;
use MongoDB\Collection;
use MongoDB\Database;
use think\Config;
class MongoManager extends Model
{
protected $mongoManager;
protected $mongoCollection;
protected $date;
protected $collection;
protected $prefix;
protected $config;
private static $instance;
public function __construct($collection)
{
$this->config = Config::get('db_mongo');
$this->prefix = $this->config['prefix'];
$this->collection = $this->prefix.$collection;
$this->mongoManager = new Manager($this->getUri());
$this->mongoCollection = new Collection($this->mongoManager, "admin", $this->collection);
$this->mongoDb = new Database($this->mongoManager, "admin");
$d = date('Y_m_d');
$colle = $this->prefix."counters".$d;
$mongoColl = new Collection($this->mongoManager, "admin", $colle);
if(!$this->listCollections($colle)){
$doc = array('_id'=>'recordid','sequence_value'=>0);
$mongoColl->insertOne($doc);
}
}
protected function getUri()
{
$connStr = 'mongodb://';
if($this->config['username'] && $this->config['password']){
$connStr .= $this->config['username'].':'.$this->config['password'].'@';
}
if($this->config['hostname']){
$connStr .= $this->config['hostname'];
}else{
$connStr .= '127.0.0.1';
}
if($this->config['hostport']){
$connStr .= ':'.$this->config['hostport'];
}else{
$connStr .= ':27017';
}
if($this->config['database']){
$connStr .= '/'.$this->config['database'];
}
//return getenv('MONGODB_URI') ?: 'mongodb://root:[email protected]:27017/admin';
return getenv('MONGODB_URI') ? : $connStr;
}
/**
* [listCollection 查詢數據集列表]
* @author sunlq 2018-11-06
* @return [type] [description]
*/
public function listCollections($colle)
{
$data = $this->mongoDb->listCollections();
$arr = array();
foreach($data as $document){
$doc = (array)$document;
foreach($doc as $d){
$arr[] = $d['name'];
}
}
return in_array($colle,$arr);//->toArray()
}
/**
* [createCollection 創建數據表]
* @author sunlq 2018-11-06
* @return [type] [description]
*/
public function createCollection()
{
$date = date('Y-m-d');
$collection = "records".$date;
$data = $this->mongoDb->createCollection($collection);
return $data;
}
/**
* 統計函數
* @author sunlq 2018-11-01
* @param array $condition [description]
* @param string $operation [description]
* @param array $fields [description]
* @param string $id [description]
* @return [type] [description]
*/
public function min($condition=array(),$operation="min", $fields=array(),$id="id")
{
$field = array();
//$matchArr = array('dmac'=>"$dmac",'mac'=>"$mac");
$fields = array('dmac'=>'dmac','mac'=>'mac');
foreach($fields as $k => $val){
$field[$k] = '$'.$val;
}
$command = array(
"aggregate" => $this->collection,
"pipeline" => array(
//'$match'=> $condition,
array('$group'=>array('_id'=>$field,'value'=>array('$'.$operation=>'$'.$id),'count'=>array('$sum'=>1)))
)
);
if(!empty($condition)){
$command['pipeline']['$match'] = $condition;
}
$res = $this->mongoDb->command($command);
$arr = array();
foreach($res as $document){
$str = json_encode($document);
$arr = json_decode($str,true);
}
return $arr['result'];
}
/**
* [select 查詢數據]
* @author sunlq 2018-11-07
* @param array $filter [description]
* @param integer $page [description]
* @param integer $pageSize [description]
* @param array $projection [description]
* @return [type] [description]
*/
public function select($filter=array(),$page=1,$pageSize=0,$projection=array(),$distinct=false){
$skip = 0;
if($page && $pageSize){
$skip = ($page-1) * $pageSize;
}
$pages = [
"skip" => $skip,
"limit" => $pageSize
//"sort" => ["id" => $sort]
];
$options = [
'projection' => $projection
];
if($pageSize){
$options = array_merge($options, $pages);
}
$list = $this->mongoCollection->find($filter, $options);
$data['list'] = $list->toArray();
$data['countData'] = $this->count($filter);
return $data;
}
/**
* [insertOne 插入一條數據]
* @author sunlq 2018-11-06
* @param [type] $document [description]
* @return [type] [description]
*/
public function insertOne($document)
{
$data = $this->mongoCollection->insertOne($document);
return $data->getInsertedCount();
}
public function insertMany($document)
{
$data = $this->mongoCollection->insertMany($document);
//return $data->isAcknowledged() && $data->getInsertedCount();
return $data;
}
public function updateDataById($filter, $document)
{
$data = $this->mongoCollection->updateOne($filter, $document);
return $data['writeResult']['nMatched'] && $data['writeResult']['nModified'];
}
public function findOne($filter=[],$options=[])
{
$data = $this->mongoCollection->findOne($filter,$options);
return $data;
}
public function find($filter=[],$options=[])
{
$data = $this->mongoCollection->find($filter,$options);
return $data->toArray();
}
public function count($filter=[],$options=[])
{
$data = $this->mongoCollection->count($filter,$options);
return $data;
}
/**
* [createIndexs 創建索引]
* @author sunlq 2018-11-09
* @param array $filter [description]
* @param array $options [description]
* @return [type] [description]
*/
public function createIndexes($indexes=[],$options=[])
{
$data = $this->mongoCollection->createIndexes($indexes,$options);
return $data;
}
public function listIndexes($options=[])
{
$data = $this->mongoCollection->listIndexes($options);
return $data;
}
/**
* [dropCollection 刪除數據集]
* @author sunlq 2018-11-12
* @return [type] [description]
*/
public function dropCollection()
{
$res = $this->mongoDb->dropCollection($this->collection);
return $res;
}
}