[英]Spring Data Aggregate Query
Team, 球队,
I am new to Spring data for mongodb. 我是mongodb的Spring数据的新手。 I am trying to learn the Spring data code for aggregation query.
我正在尝试学习用于聚合查询的Spring数据代码。 but most of the tutorial shown only the simple examples.
但是大多数教程只显示了简单的示例。 Could you please help me build the spring data code for the given complex aggregate example.
能否请你帮我打造为给定的复杂集合体例如弹簧数据的代码。
SCHEMA:
{
"s" : "CB",
"c" : "REQ_RCV",
"e" : "cta_sms_click",
"st" : "i",
"b" : "UB",
"a" : "account-1",
"u" : "b1_h1_d1_m1_user_2",
"c#" : "b1_h1_d1_m1_cr-2",
"@" : ISODate("2016-10-01T06:03:00.000Z"),
"@h" : "16100106",
"@d" : "161001",
"@m" : "1610"
}
QUERY:
db.COLLECTION_NAME.aggregate([
{$match:{"st":"i","@":{$gte : new ISODate("2015-10-01T06:00:00Z"), $lte : new ISODate("2017-10-02T10:00:00Z")}, "c":"REQ_RCV"}},
{$group:{_id:{"b":"$b", "HOURLY":"$@h"}, count:{$sum:1}}},
{$project : {_id:0, "BUCKET":"$_id.b", "TIME":"$_id.HOURLY", count:1}},
{$sort:{"BUCKET":1, "TIME":1}}
]);
Complexities: 复杂性:
Initial answer from Veeram for reference: Veeram的初步答案供参考:
Aggregation agg = newAggregation(match(where("st").is("i").and("@").gte(start_date).lte(end_date).and("c").is("REQ_RCV")),
group(fields("b").and("HOURLY", "$@h")).count().as("count"),
project(fields("count").and("BUCKET","$_id.b").and("TIME", "$_id.HOURLY")),
sort(ASC, "BUCKET", "TIME"));
So there is probably a more spring-data way of doing this but what im doing is wiring the MongoTemplate and using that to the Aggreagation query. 因此,可能有更多的弹簧数据方式来执行此操作,但即时通讯正在连接MongoTemplate并将其用于Aggreagation查询。 My Example us much simpler than what you want to do but maybe it can help :
我的示例我们比您想做的要简单得多,但也许可以帮助您:
@Service
public class AClass implements CategoryStructureService {
@Autowired
private MongoTemplate mongoTemplate ;
......
private int method(CategoryStructureKey csKey) {
Aggregation agg = Aggregation.newAggregation(Aggregation.match(Criteria
.where("_id").is(csKey)), Aggregation.unwind("fields"),
Aggregation.project("fields").andExclude("_id"), Aggregation
.sort(Direction.DESC, "fields.index"), Aggregation
.limit(1));
AggregationResults<MultiValuedFieldContainer> field = mongoTemplate
.aggregate(agg, CategoryStructure.class,
MultiValuedFieldContainer.class);
....
}
I did this with an old spring-boot project, having this as a dependency 我用一个旧的spring-boot项目来做这个,把它作为依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
So it should work. 因此它应该工作。
You can find a better example of MongoTemplate here https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/ 您可以在这里找到更好的MongoTemplate示例https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/
And some other Agrregation examples https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java 还有其他一些Agrregation示例https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation /AggregationTests.java
Hope this helps 希望这可以帮助
You can try something like below. 您可以尝试以下类似方法。 Replace with your date values and collection name.
用您的日期值和集合名称替换。
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.query.Criteria.where;
Aggregation agg = newAggregation(match(where("st").is("i").and("@").gte(new Date()).lte(new Date()).and("c").is("REQ_RCV")),
group(fields("b").and("HOURLY", "$@h")).count().as("count"),
sort(ASC, Aggregation.previousOperation()));
List<BasicDBObject> dbObjects = mongoOperations.aggregate(agg, "collection_name", BasicDBObject.class).getMappedResults();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.