2017-01-16 77 views
1

团队,春季数据综合查询

我是新的Spring数据MongoDB。我正在尝试学习聚合查询的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":"[email protected]"}, count:{$sum:1}}}, 
     {$project : {_id:0, "BUCKET":"$_id.b", "TIME":"$_id.HOURLY", count:1}}, 
     {$sort:{"BUCKET":1, "TIME":1}}      
    ]); 

复杂性:

  1. $匹配具有muliple指标分析
  2. $项目已进入下_id组的内场
  3. ,因为它变化的结果不能被映射到一个类基于$项目字段的变化。最终我会 喜欢把它映射到java.util.HashMap,这样我就可以把任何字段 置于$ project中。那可能吗?

从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", "[email protected]")).count().as("count"), 
        project(fields("count").and("BUCKET","$_id.b").and("TIME", "$_id.HOURLY")), 
        sort(ASC, "BUCKET", "TIME")); 
+0

@chridam你能帮我吗? –

回答

1

您可以尝试像下面的东西。用您的日期值和集合名称替换。

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", "[email protected]")).count().as("count"), 
      sort(ASC, Aggregation.previousOperation())); 

List<BasicDBObject> dbObjects = mongoOperations.aggregate(agg, "collection_name", BasicDBObject.class).getMappedResults(); 
+0

谢谢! Veeram ventrathu !!! –

+0

一个查询。在'项目'中声明名字像“BUCKET”和“TIME”是必须在'sort'中调用它的?这是不可能写'排序(ASC,“$ _id.b”,“$ _id.HOURLY”)? –

+0

更新了答案。对不起,我应该看到了。你可以使用'previousOperation()'进行排序,并保存对之前的组“_id”的引用。 – Veeram

1

所以有可能是这样做的更多的弹簧数据的方式,但什么IM做的是布线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); 
     .... 

     } 

我这样做是与老春启动的项目,有此作为一个依赖

<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

希望这有助于