<?php
/**
* 适用php7以下版本
* 此文件用来每天定时执行,把mongodb里面runoob表中昨日的数据导入到everyday表中
* $Date 可作为后台查询数据的日期 (格式例如:2018-11-01)
*/
date_default_timezone_set('PRC');
error_reporting(E_ERROR | E_PARSE);
//判断,只允许定时任务在4:30到8:30中会进行插入数据
$time=time();
$a =strtotime(date("Y-m-d 4:30:00"));
$b =strtotime(date("Y-m-d 8:30:00"));
if(($time < $a) || ($time > $b)){
echo '不在指定时间内执行';die;
}
//查询范围,前一天8点到今天8点的订单数据
$start_time=date("Y-m-d 08:00:00",strtotime("-1 day"));
$end_time=date("Y-m-d 08:00:00");
$start_time=strtotime($start_time);
$end_time=strtotime($end_time);
//记录插入新mongo哪天的数据,可直接查询使用
$Date=date("Y-m-d",strtotime("-1 day"));
$linkmongo = 'mongodb://127.0.0.1:27017';
$m =new MongoClient($linkmongo);
$db_mongo = $m->test;
$collection = $db_mongo->runoob;
$where = ['indent_time'=>['$gte'=>$start_time,'$lte'=>$end_time]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['indent_time'=>1]
];
$result_object = $collection->find($where);
$result=array();
foreach ($result_object as $key=>$value) {
$result[$key]=$value;
}
$result=array_merge($result);
$uid=array_unique(array_column($result, 'user_id'));
$uid=array_merge($uid); //重置key值
$mongo = $db_mongo->everyday;//重新选择数据表
if(!empty($result)){
$count1=0;
$count2=0;
foreach ($uid as $k1 => $v1) {
$data = array();
foreach ($result as $k => $v) {
if($v['user_id']==$v1){
if($v['indent_type']=='1'){
$data['Date']=$Date; //哪天的数据
$data['user_id'] = $v['user_id'];
$data['user_name'] = $v['user_name'];
$data['user_remark_name']=$v['user_remark_name'];
$data['user_nickname'] = $v['user_nickname'];
$data['user_score'] = $v['user_score'];
$data['user_admin_id'] = $v['user_admin_id'];
$data['indent_userid'] = $v['indent_userid'];
$data['indent_admin_id'] = $v['indent_admin_id'];
$data['indent_profit_loss'] += $v['indent_profit_loss']; //累加
$data['indent_profit'] += $v['indent_profit']; //累加
$data['indent_total'] += $v['indent_total']; //累加
$data['indent_one_money'] += $v['indent_one_money']; //累加
$data['indent_odds'] = $v['indent_odds'];
$data['indent_od'] = $v['indent_od'];
$data['admin_account'] = $v['admin_account'];
$data['indent_admin_zs'] = $v['indent_admin_zs'];
$data['indent_test'] = $v['indent_test'];
$data['indent_time'] = $v['indent_time'];
$data['indent_type'] = $v['indent_type'];
$data['indent_count'] += $v['indent_count']; //累加
}
}
}
if(!empty($data)){
$mongo->insert($data);
$count1=$count1+1;
}
}
foreach ($uid as $k2 => $v2) {
$datas = array();
foreach ($result as $k => $v) {
if($v['user_id']==$v2){
if($v['indent_type']=='2'){
$datas['Date']=$Date; //哪天的数据
$datas['user_id'] = $v['user_id'];
$datas['user_name'] = $v['user_name'];
$datas['user_remark_name']=$v['user_remark_name'];
$datas['user_nickname'] = $v['user_nickname'];
$datas['user_score'] = $v['user_score'];
$datas['user_admin_id'] = $v['user_admin_id'];
$datas['indent_userid'] = $v['indent_userid'];
$datas['indent_admin_id'] = $v['indent_admin_id'];
$datas['indent_profit_loss'] += $v['indent_profit_loss']; //累加
$datas['indent_profit'] += $v['indent_profit']; //累加
$datas['indent_total'] += $v['indent_total']; //累加
$datas['indent_one_money'] += $v['indent_one_money']; //累加
$datas['indent_odds'] = $v['indent_odds'];
$datas['indent_od'] = $v['indent_od'];
$datas['admin_account'] = $v['admin_account'];
$datas['indent_admin_zs'] = $v['indent_admin_zs'];
$datas['indent_test'] = $v['indent_test'];
$datas['indent_time'] = $v['indent_time'];
$datas['indent_type'] = $v['indent_type'];
$datas['indent_count'] += $v['indent_count']; //累加
}
}
}
if(!empty($datas)){
$mongo->insert($datas);
$count2=$count2+1;
}
}
$count=$count1+$count2;
echo $Date."一共导入".$count."条数据。";die;
}else{
echo $Date."没有数据可导入";die;
}
//打印数据使用dump方法
function dump($var, $echo=true, $label=null, $strict=true) {
$label = ($label === null) ? '' : rtrim($label) . ' ';
if (!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, true);
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
} else {
$output = $label . print_r($var, true);
}
} else {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
}
}
if ($echo) {
echo($output);
return null;
}else
return $output;
}
?>
php7以下操作mongodb数据库语法
于 2020-01-09 16:12:40 首次发布