zoukankan      html  css  js  c++  java
  • Yii2的mongodb的聚合操作

    最近项目使用到mongodb的聚合操作,但是yii文档中对这方面资料较少,记录下

            $where['created_time'] = ['$gt' => "$start_date_str", '$lte' => "$end_date_str"];
            $where['token'] = ['$ne' => null];
            $where['app'] = $app;
            $where['source_type'] = $source_type;
            $loanmarket_info = Loanmarket::find()->asArray()->orderBy('created_time DESC')->all();
            if (!empty($keyword)) {
                $loanmarket_info = Loanmarket::find()->where(['or',['like','name',$keyword],['like','number',$keyword]])->asArray()->orderBy('created_time DESC')->all();
                $keyword_arr = array_reduce($loanmarket_info, function ($result, $value) {
                    return array_merge($result, array_values($value));
                }, array());
                if (!empty($keyword_arr)) {
                    $where['lm_number'] = ['$in' => $keyword_arr];
                }
            }
    
            $aggregate = [
                ['$match' => $where]    // 按照创建时间筛选,同时 token 不为空
                , ['$group' => ["_id" => ["lm_number" => '$lm_number', "source_type" => '$source_type', "app" => '$app', "token" => '$token']]]  // token 去重
                , ['$group' => ["_id" => ["lm_number" => '$_id.lm_number', "source_type" => '$_id.source_type', "app" => '$_id.app'], "COUNT(token)" => ['$sum' => 1]]]  // 统计UV
                , ['$project' => ["_id" => 0, "lm_number" => '$_id.lm_number', "source_type" => '$_id.source_type', "app" => '$_id.app', "uv" => '$COUNT(token)']] // 显示相应的字段
            ];
    //        $query = new yiimongodbQuery;
    //        $data_arr = $query->from(['数据库名','数据表名'])->offset(3)->limit(5)->aggregate($aggregate);
            $data_arr = MongoBizEventReport::getCollection()->aggregate($aggregate);
    

      

  • 相关阅读:
    应用实例-最大子列和问题
    什么是算法
    什么是数据结构
    数据结构与算法-Python/C(目录)
    collections模块
    色彩缤纷的Python(改变字体颜色及样式)
    取数组中一段值的算法(转)
    开发者必备,超实用的PHP代码片段(转)
    php中数组自定义排序
    rand值出现负数的解决方案
  • 原文地址:https://www.cnblogs.com/two-bees/p/10483435.html
Copyright © 2011-2022 走看看