<?phpuse MongoDB\BSON\UTCDateTime;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;class MongoDB
{
protected $connection = 'mongodb://username:password@hostname:hostport/?authSource=database';protected//mongodb连接管理$Manager,//数据库$database = 'GameLog',//集合(表)$table = 'GameLog0';public function __construct(){
$this->Manager = new Manager($this->connection);}/*** 获取数据* @param string|array $pipeline 条件配置* @param bool $isPage 是否分页* @return mixed* @throws \MongoDB\Driver\Exception\Exception*/public function getData($pipeline, $isPage = false){
//组装查询语句$options = $this->getOptions($pipeline);if ($isPage) {
$total = $this->getCount($options);$options = $this->mongoPage($options);}//执行查询获取数据$data = $this->executeCommand($options);$return = ['data' => $data,'options' => $options];if ($isPage) $return['total'] = $total;return $return;}/*** 拼装查询options* @param $group* @return array*/protected function getOptions($pipeline){
$options = ['aggregate' => $this->table,'pipeline' => [['$match' => $this->getMongoCondition()]],'cursor' => new \stdClass(),];array_push($options['pipeline'], ...$pipeline);return $options;}/*** mongo command查询* @param $options* @return array* @throws \MongoDB\Driver\Exception\Exception*/protected function executeCommand($options){
$command = new Command($options);$result = $this->Manager->executeCommand($this->database, $command)->toArray();return $result;}/*** mongo 分页* @param $options* @return mixed*/protected function mongoPage($options){
$page = $_GET['page'] ?? 1;$limit = $_GET['limit'] ?? 10;$page = [['$limit' => $page * $limit],['$skip' => ($page - 1) * $limit],];array_push($options['pipeline'], ...$page);return $options;}/*** 获取总条数* @param $options* @return int* @throws \MongoDB\Driver\Exception\Exception*/public function getCount($options){
array_push($options['pipeline'], ['$group' => ['_id' => null, 'count' => ['$sum' => 1]]]);return $this->executeCommand($options)[0]->count;}/*** date范围格式化* @param datetime $dateTimes 日期* @param bool $isToTime 是否转时间戳* @return mixed*/protected function getDateTime($dateTimes, $isToTime = false){
if (!is_array($dateTimes)) {
$dateTimes = explode(' - ', $dateTimes);}foreach ($dateTimes as &$date) {
$date = $isToTime ? (int)strtotime($date) : date('Y-m-d H:i:s', strtotime($date));}return $dateTimes;}/*** 获取mongo条件* @return array*/protected function getMongoCondition(){
$condition = ['ClientType' => ['$gt' => 0]];$get = $_GET;if (!empty($get['Time'])) {
$range_time = $this->getDateTime($get['Time'], true);$condition['TimeStamp'] = ['$gte' => $range_time[0],'$lt' => $range_time[1],];}if (!empty($get['KindID'])) {
$condition['KindID'] = ['$eq' => (int)$get['KindID']];}if (!empty($get['ServerId'])) {
$condition['ServerID'] = ['$eq' => (int)$get['ServerId']];}if (!empty($get['UserID'])) {
$condition['UserID'] = ['$eq' => (int)$get['UserID']];}return $condition;}
}$Mongo = new MongoDB();$UTCDateTime = new UTCDateTime(0);
$repair_time = 3600000 * 8;//修复时差
$pipeline = [['$match' => ['KindID' => ['$in' => [204, 220]],'$or' => [['KindID' => 204],['KindID' => 220]]]],['$project' => ['timeToDate' => ['$dateToString' => ['format' => "%Y-%m-%d",'date' => ['$add' => [$UTCDateTime,['$multiply' => ['$TimeStamp', 1000]], $repair_time]]]],'WinGold' => '$WinGold','BetonGold' => '$BetonGold','KindID' => '$KindID']],['$group' => ['_id' => ['date' => '$timeToDate', 'KindID' => '$KindID'],//下注次数'count' => ['$sum' => 1],//输赢金额'Amount' => ['$sum' => '$WinGold'],'BetonGold' => ['$sum' => '$BetonGold'],'KindID' => ['$last' => '$KindID'],'date' => ['$last' => '$timeToDate']]],['$sort' => ['_id.date' => -1]]
];echo '<pre>';
$data = $Mongo->getData($pipeline);
var_dump($data);
想转型做一名爬虫架构师的朋友,推荐学习!
扫码下单输优惠码【csdnfxzs】再减5元,比官网还便宜!