库里的数据

简单分组查询

      //封装查询条件List<AggregationOperation> operations = new ArrayList<>();//根据用户id in查询operations.add(Aggregation.match(Criteria.where("from").in(ids)));//然后分组operations.add(Aggregation.group("from").count().as("count"));Aggregation aggregation = Aggregation.newAggregation(operations);AggregationResults<CustomerStatisticsDTO> aggregateData = imMongoTemplate.aggregate(aggregation, "chat_history", CustomerStatisticsDTO.class);List<CustomerStatisticsDTO> data = aggregateData.getMappedResults();System.out.println(JSON.toJSONString(data));
            //根据pageName和userIdQuery query = new Query(Criteria.where("pageName").is(TaskTypeEnum.VIEW_SUCCESS_SERVICE.getPageName()).and("userId").is(v));//创建时间倒叙 取第一条query.with(Sort.by(Sort.Direction.DESC, "createTime")).limit(1);CustomerStatisticsDTO statisticsDto = mongoTemplate.findOne(query, CustomerStatisticsDTO.class, "buried_point");

复杂分组查询

库里的数据

1.根据pageName字段查询和时间区间和格式化时间类型的查询

  //封装查询条件List<AggregationOperation> operations = new ArrayList<>();//检索pageName字段operations.add(Aggregation.match(Criteria.where("pageName").is(TaskTypeEnum.VIEW_SUCCESS_SERVICE.getPageName()).//大于等于and("createTime").gte(new Date(1638316845000L))));//小于等于operations.add(Aggregation.match(Criteria.where("createTime").lte(new Date())));//date类型 转成字符串 $dateToString 函数operations.add(Aggregation.project().andExpression("{$dateToString:{ format:'%Y-%m-%d',date: '$createTime'}}").as("createTime"));Aggregation aggregation = Aggregation.newAggregation(operations);AggregationResults<CustomerStatisticsDTO> aggregateData = mongoTemplate.aggregate(aggregation, "buried_point", CustomerStatisticsDTO.class);System.out.println(JSON.toJSONString(aggregateData.getMappedResults()));
format:需要返回的日期式,日期格式通常为以:%Y Year (4 digits, zero padded) 0000-9999
%m Month (2 digits, zero padded) 01-12
%d Day of Month (2 digits, zero padded) 01-31
%H Hour (2 digits, zero padded, 24-hour clock) 00-23
%M Minute (2 digits, zero padded) 00-59
%S Second (2 digits, zero padded) 00-60
%L Millisecond (3 digits, zero padded) 000-999
%j Day of year (3 digits, zero padded) 001-366
%w Day of week (1-Sunday, 7-Saturday) 1-7
%U Week of year (2 digits, zero padded) 00-53
%% Percent Character as a Literal$dateToString 可以替代为

$dayOfYear: 返回该日期是这一年的第几天。(全年366天)
$dayOfMonth: 返回该日期是这一个月的第几天。(1到31)
$dayOfWeek: 返回的是这个周的星期几。(1:星期日,7:星期六)
$year: 返回该日期的年份部分 $month: 返回该日期的月份部分(between 1and12.)
$week: 返回该日期是所在年的第几个星期(between 0and53)
$hour: 返回该日期的小时部分 $minute: 返回该日期的分钟部分
$second: 返回该日期的秒部分(以0到59之间的数字形式返回日期的第二部分,但可以是60来计算闰秒。)
$millisecond:返回该日期的毫秒部分(between 0and999.)

2.先根据pageName字段,然后取时间区间,然后把时间格式化为小时,然后分组,倒叙,求一段时间内的那个小时内用户访问量最大

    //封装查询条件List<AggregationOperation> operations = new ArrayList<>();//检索pageName字段operations.add(Aggregation.match(Criteria.where("pageName").is(TaskTypeEnum.VIEW_SUCCESS_SERVICE.getPageName()).//大于等于and("createTime").gte(new Date(1638316845000L))));//小于等于operations.add(Aggregation.match(Criteria.where("createTime").lte(new Date())));//只看小时operations.add(Aggregation.project().andExpression("{$hour:{date: '$createTime'}}").as("createTime"));//分组 之后count求数量operations.add(Aggregation.group("createTime").count().as("count"));//根据count数量倒叙operations.add(Aggregation.sort(Sort.by(Sort.Direction.DESC, "count")));//取第一条operations.add(Aggregation.limit(1));Aggregation aggregation = Aggregation.newAggregation(operations);AggregationResults<CustomerStatisticsDTO> aggregateData = mongoTemplate.aggregate(aggregation, "buried_point", CustomerStatisticsDTO.class);System.out.println(JSON.toJSONString(aggregateData.getMappedResults()));

3.多字段分组之后结构会发生改变

        //封装查询条件List<AggregationOperation> operations = new ArrayList<>();//检索pageName字段operations.add(Aggregation.match(Criteria.where("pageName").is(TaskTypeEnum.VIEW_SUCCESS_SERVICE.getPageName()).//大于等于and("createTime").gte(new Date(1638316845000L))));//小于等于operations.add(Aggregation.match(Criteria.where("createTime").lte(new Date())));//只看小时operations.add(Aggregation.project().andInclude("userId").andExpression("{$dateToString:{ format:'%H',date: '$createTime'}}").as("createTimeStr"));//分组 之后count求数量operations.add(Aggregation.group("createTimeStr", "userId").count().as("count"));//重新定义字段//operations.add(Aggregation.project("createTimeStr", "userId", "count"));Aggregation aggregation = Aggregation.newAggregation(operations);AggregationResults<CustomerStatisticsDTO> aggregateData = mongoTemplate.aggregate(aggregation, "buried_point", CustomerStatisticsDTO.class);System.out.println(JSON.toJSONString(aggregateData.getMappedResults()));

加上这行代码,就能重新定义列

operations.add(Aggregation.project("createTimeStr", "userId", "count"));

如图:多出来了两列

Mongodb分组查询相关推荐

  1. MongoDB分组查询,聚合查询,以及复杂查询

    准备数据 from pymongo import MongoClient import datetimeclient=MongoClient('mongodb://localhost:27017') ...

  2. MongoDB sql分组查询,以及Java操作MongoDB分组查询

    一.MongoDB SQL 1.使用group方法分组(写法一) select c_sender,c_sendtime,total,dep_ids,avg from goods_order group ...

  3. MongoDB分组查询、聚合查询

  4. MongoDB 分组统计

    [摘要] MongoDB 在进行分组统计时如果面对一些比较复杂的计算情况,往往会遇到 shell 脚本过于复杂的问题.而集算器 SPL 语言,则因其有丰富的函数库及易用性恰好能弥补 Mongo 这方面 ...

  5. MongoDB聚合查询 Pipeline 和 MapReduce

    MongoDB聚合查询 MongoDB聚合查询 什么是聚合查询 Pipeline聚合管道方法 聚合流程 详细流程 聚合语法 常用聚合管道 $count $group $match $unwind $p ...

  6. MongoDB聚合查询

    MongoDB高手课_MongoDB_NoSQL-极客时间极客时间推出的MongoDB高手课是帮助互联网从业者学习MongoDB.NoSQL的在线课程,极客时间是面向IT领域的知识服务产品,致力于帮助 ...

  7. MongoDB高级查询介绍

    title: MongoDB高级查询介绍 date: 2017-12-3 22:14:19 tags: MongoDB MongoDB查询 categories: 数据库 在几乎所有的项目中对数据库的 ...

  8. mysql分组和where条件查询_【MySQL】:分组查询where和having

    分组查询 之前学习聚合函数,知道聚合函数在默认情况下,将会把所有的记录当成一组,让我们在对列求值,计算时更方便了一些. 但是,在某些情况下,我们需要显式地对记录进行分组,使用的是group by [c ...

  9. MySQL数据库分组查询group by(having)

    1. 分组查询介绍 分组查询就是将查询结果按照指定字段进行分组,字段中数据相等的分为一组. 分组查询基本的语法格式如下: GROUP BY 列名 [HAVING 条件表达式] [WITH ROLLUP ...

最新文章

  1. pdb+ipdb 调试 Python代码
  2. mybatis之一对多
  3. Linux学习:目录遍历函数
  4. iOS---------- MBProgressHUD (1.0.0)的变动
  5. SpringBoot集成MyBatis-Plus分页插件
  6. 有望年前发布?魅族16s Pro Plus曝光:下半年旗舰担当
  7. mysql+installer+community+5.7.9_win10系统,mysql-installer-community-5.7.19.0.msi安装
  8. PHP文件系统-文件路径操作和权限
  9. “康园圈--互联网+校园平台“项目之拓展手机客户端
  10. fedora oracle使用,【解决方案】连接到Fedora 14上的Oracle数据库服务器
  11. rss opml_分享我的OPML,所有人都在做
  12. android 行居中,android自己定义换行居中CenterTextView(示例代码)
  13. Android上传图片的方式
  14. 手动实现循环神经网络RNN,神经网络rnn是什么意思
  15. (数据库-MySQL) Date 函数
  16. vue中怎么获取元素
  17. Translate Aticle
  18. 安科瑞:列头柜、监控系统、触摸屏的数据中心机房配电方案
  19. P7-Windows与网络基础-虚拟机基本操作
  20. [论文阅读笔记17]A Survey on Knowledge Graph-Based Recommender Systems

热门文章

  1. python 判断两个列表间是否具有包含关系以及获取子列表在父列表中的索引
  2. fsck.ext3:unable to resolve 'LABLE=/home'
  3. UOJ #11. 【UTR #1】ydc的大树
  4. 苹果手机验真假_别再被坑了,这3大技巧可辨别真假iPhone,懂得话赚大了
  5. 在数据库使用期间创建OMF(Oracle Managed Files,Oracle管理的文件)
  6. 撰写全英文EI会议论文值得注意的要点!
  7. ThreeJS - 动态更换fbx模型的某个子Mesh现有的纹理贴图为指定的纹理贴图
  8. 可视计算机应用李桂清,华南理工大学研究生导师介绍---李桂清
  9. 大学计算机网课怎么上,大学计算机课程学习路线应该是怎么样的呢?
  10. Flink大数据计算框架