import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
第一种,使用mongoTemplate.findDistinct去重,不支持排序,即使你的query条件带sort排序方法。mongoTemplate.findDistinct去重,会使排序失效。
优点:查询效率高
缺点:只返回单一字段。不知多字段返回。不能使用排序,不推挤使用
Query query = new Query();
query.addCriteria(Criteria.where("deviceId").is(getListParam.getDeviceId())).with(Sort.by(Sort.Order.desc("startDate")));
List<RedPacketDeviceRelation> list = mongoTemplate.find(query, RedPacketDeviceRelation.class);
List<RedPacketDeviceRelation> activeCodes = mongoTemplate.findDistinct(query, "activeCode", "redPacketDeviceRelation",RedPacketDeviceRelation.class, RedPacketDeviceRelation.class);
第二种,使用mongoTemplate.aggregate去重,支持排序。推荐使用
优点:可指定返回类型。支持排序
缺点:排序是查询效率会变的非常低
TypedAggregation tagg = TypedAggregation.newAggregation(RedPacketDeviceRelation.class,
         Arrays.asList(
//筛选条件
TypedAggregation.match(Criteria.where("deviceId").is(getListParam.getDeviceId())),
//分组过滤条件,first,as里最后包含展示的字段
TypedAggregation.group("activeCode").first("activeCode").as("activeCode").first("startDate").as("startDate"),
//挑选需要字段
          TypedAggregation.project("activeCode", "startDate"),
//排序字段
TypedAggregation.sort(Sort.by(Sort.Order.desc("startDate")))
         )
);
AggregationResults result111 = mongoTemplate.aggregate(tagg, RedPacketDeviceRelation.class);
List<RedPacketDeviceRelation> rd = result111.getMappedResults();
log.debug("排序后的mongoTemplate.group列表1:"+rd); 第三种,用法和第二种类似
Aggregation agg = Aggregation.newAggregation(
// 挑选所需的字段,类似select *,*所代表的字段内容
Aggregation.project("activeCode", "startDate","packetType"),
// sql where 语句筛选符合条件的记录
Aggregation.match(Criteria.where("deviceId").is(getListParam.getDeviceId())),
// 分组条件,设置分组字段
Aggregation.group("activeCode").first("activeCode").as("activeCode").first("startDate").as("startDate"),
// 排序(根据某字段排序 倒序)
Aggregation.sort(Sort.Direction.DESC,"startDate"),
// 重新挑选字段
Aggregation.project("activeCode")
);
AggregationResults<JSONObject> results = mongoTemplate.aggregate(agg, "redPacketDeviceRelation", JSONObject.class);
List<JSONObject> a= results.getMappedResults();
log.debug("排序后的code列表2:[{}]",results);
原创文章,引用注明出处:https://www.cnblogs.com/guangxiang/p/12366017.html
												

java使用mongoTemplate去重排序查询的更多相关文章

  1. app微信支付-java服务端接口 支付-查询-退款

    个人不怎么看得懂微信的文档,看了很多前辈的写法,终于调通了,在这里做一下记录. 首先来定义各种处理类(微信支付不需要特殊jar包,很多处理需要自己封装,当然也可以自己写完打个jar包) 参数要用jdo ...

  2. MongoTemplate复合条件查询

    分. 排序.按时间查询 Query query = new Query();        //必须条件        Criteria c = Criteria.where("VINID& ...

  3. 分享一种容易理解的js去重排序方法

    <script> var arr=[1,8,6,4,88,22,99,4,6,86,5,58,89,5]; //先使用sort()函数去重 var a=arr.sort(function ...

  4. linux下批量修改存有超大数据量IP文件中的IP内容以及去重排序

    作为一个linux的学徒,分享一下自己解决这个小问题的心得,在处理这个问题时使用了一个小技巧感觉很适用,个人发觉linux的终端真滴是非常强大,下面就详细地介绍这个问题以及解决办法吧 问题描述:由于要 ...

  5. Java内存访问重排序笔记

    >>关于重排序 重排序通常是编译器或运行时环境为了优化程序性能而采取的对指令进行重新排序执行的一种手段. 重排序分为两类:编译期重排序和运行期重排序,分别对应编译时和运行时环境. > ...

  6. 对于Oracle中分页排序查询语句执行效率的比较分析

    转自:http://bbs.csdn.net/topics/370033478 对于Oracle中分页排序查询语句执行效率的比较分析 作者:lzgame 在工作中我们经常遇到需要在Oracle中进行分 ...

  7. Java线性表的排序

    Java线性表的排序 ——@梁WP 前言:刚才在弄JDBC的时候,忽然觉得order-by用太多了没新鲜感,我的第六感告诉我java有对线性表排序的封装,然后在eclipse里随便按了一下“.” ,哈 ...

  8. Solr4.8.0源码分析(6)之非排序查询

    Solr4.8.0源码分析(6)之非排序查询 上篇文章简单介绍了Solr的查询流程,本文开始将详细介绍下查询的细节.查询主要分为排序查询和非排序查询,由于两者走的是两个分支,所以本文先介绍下非排序的查 ...

  9. java序列化对象 插入、查询、更新到数据库

    java序列化对象 插入.查询.更新到数据库 : 实现代码例如以下: import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...

随机推荐

  1. centos下离线安装zip和unzip

    首先如果你的centos可以联网,那可以不用看了,直接yum install -y zip unzip就行,非常的痛快! 如果不能联网,像我一样,只能用vpn连上去,做了点限制.那就非常烦了,yum了 ...

  2. linux服务器常用操作和命令

    1. 什么是linux服务器load average? Load是用来度量服务器工作量的大小,即计算机cpu任务执行队列的长度,值越大,表明包括正在运行和待运行的进程数越多.参考资料:http://e ...

  3. html的适配

    html值得一说的应该就是适配 !!适配是与手机同时存在的 写好一个页面在手机端打开,会发现这个页面显示很小,那是因为设备的视口宽度viewport不等于设备宽度device-width,而页面是根据 ...

  4. 04.swoole学习笔记--webSocket服务器

    <?php //创建webSocket服务器 $serv=); //获取请求 //on //open 建立连接 $serv:服务器 $request:客户端信息 $serv->on('op ...

  5. java.io.IOException: Error: JSP Buffer overflow

    错误 jsp页面报错如下: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java ...

  6. iPhone Safari下iframe不显示滚动条无法滚动的解决方法。iframe的坑!

    <div class="dataTables_wrapper" style="-webkit-overflow-scrolling:touch;overflow:a ...

  7. 五、Vue:使用axios库进行get和post、用拦截器对请求和响应进行预处理、Mock(数据模拟)

    一.axios [应用]进行请求和传表单 [axios中文档]:https://www.kancloud.cn/yunye/axios/234845 [vue-axios]:https://cn.vu ...

  8. 提升Windows系统舒适度软件

    1.Geek Uninstaller 卸载软件 2.PotPlayer 无广告播放器

  9. Codeforces1243C Tile Painting

    原题面:https://codeforces.com/contest/1243/problem/C 题目大意:给定数字n,有n个方块,第i个和第j个之间的距离(abs(i-j))如果是n的因子,那么第 ...

  10. WordPress 更新中断故障

    WordPress 更新中断故障 WordPress更新中断后显示:Briefly unavailable for scheduled maintenance. Check back in a min ...