2017-01-16 81 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

希望這有助於