队
我是mongodb的Spring数据新手。我正在尝试学习用于聚合查询的Spring数据代码。但是大多数教程只显示了简单的例子。您能请帮我构建给定复杂聚合示例的spring数据代码。
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}}
]);
复杂性:
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"));
答案 0 :(得分:2)
所以可能有更多的弹簧数据方式来做这个,但我正在做的是连接MongoTemplate并将其用于Aggreagation查询。我的例子比你想做的更简单,但也许它可以帮助:
@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);
....
}
我使用旧的spring-boot项目做了这个,将其作为依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
所以它应该有用。
你可以在这里找到一个更好的MongoTemplate示例 https://www.mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/
以及其他一些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
希望这有帮助
答案 1 :(得分:2)
您可以尝试以下内容。替换为您的日期值和集合名称。
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-4">
<div class="thumbnail">
<img class="img-responsive" src="//placehold.it/800" >
</div>
</div>
<div class="col-xs-4">
<div class="thumbnail">
<img class="img-responsive" src="//placehold.it/800" >
</div>
</div>
<div class="col-xs-4">
<div class="thumbnail">
<img class="img-responsive" src="//placehold.it/800" >
</div>
</div>
</div>
<div class="row">
<div class="col-xs-4">
<div class="thumbnail">
<img class="img-responsive" src="//placehold.it/800">
</div>
</div>
<div class="col-xs-4">
<div class="thumbnail">
<img class="img-responsive" src="//placehold.it/800" >
</div>
</div>
<div class="col-xs-4">
<div class="thumbnail">
<img class="img-responsive" src="//placehold.it/800" >
</div>
</div>
</div>
</div>