ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java使用mongoTemplate去重排序查询

2020-02-26 11:52:42  阅读:1068  来源: 互联网

标签:startDate TypedAggregation java Aggregation mongoTemplate activeCode 排序


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


标签:startDate,TypedAggregation,java,Aggregation,mongoTemplate,activeCode,排序
来源: https://www.cnblogs.com/guangxiang/p/12366017.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有