paulwong

          在SPRING DATA MONGODB中使用聚合統計查詢

          在SQL語句中如要做統計一般是這種方式:
          SELECT ..,SUM(1)
          FROM ..
          WHERE ..
          GROUP BY ..
          HAVING ..
          SORT ..


          在MONGODB中的架構圖



          在SPRING DATA MONGODB中是這樣寫的:
          public class VideoRepositoryImpl implements VideoRepositoryCustom{
              
              private static Logger logger = LoggerFactory.getLogger(VideoRepositoryImpl.class);
              
              @Autowired
              private MongoTemplate mongoTemplate;
              

              public List<Cat1UpdateCount> getVideoWithUpdateFrag(List<String> importantCat1List) {
                  
                  logger.info(new Date().toString());
                  
                  /**
                   * db.videos.aggregate(
                      [
                         { $match: { "frags.isnew" : true } },
                         { $unwind: "$frags" },
                         { $match: { "frags.isnew" : true } },
                         { $group: { 
                                     _id: {cat1:"$cat1"},
                                     count: { $sum: 1 },
                                     publishdate2: { $max: "$publishdate"}
                                   }
                         }
                         
                      ]
                      )
                   
          */
                  Aggregation agg = newAggregation(
                          project("frags","cat1","publishdate"),//挑選所需的字段
                          match(
                                  Criteria.where("frags.isnew").is(Boolean.TRUE)
                                  .and("cat1").in(importantCat1List)
                               ),//篩選符合條件的記錄
                          unwind("frags"),//如果有MASTER-ITEM關系的表,需同時JOIN這兩張表的,展開子項LIST,且是內鏈接,即如果父和子的關聯ID沒有的就不會輸出
                          match(Criteria.where("frags.isnew").is(Boolean.TRUE)),
                          group("cat1")//設置分組字段
                              .count().as("updateCount")//增加COUNT為分組后輸出的字段
                              .last("publishdate").as("publishDate"),//增加publishDate為分組后輸出的字段
                          project("publishDate","cat1","updateCount")//重新挑選字段
                              .and("cat1").previousOperation()//為前一操作所產生的ID FIELD建立別名
                      );

                      AggregationResults<Cat1UpdateCount> results = mongoTemplate.aggregate(agg, Video.COLLECTION_NAME, Cat1UpdateCount.class);
                      List<Cat1UpdateCount> cat1UpdateCountList = results.getMappedResults();
                  
                  return cat1UpdateCountList;
              }

          }

          其中frags的數據類型是LIST

          Cat1UpdateCount.java
          import java.io.Serializable;

          public class Cat1UpdateCount implements Serializable{

              private static final long serialVersionUID = 4240876746984930098L;
              
              private String cat1;
              
              private int updateCount;
              
              private String publishDate;

              public String getCat1() {
                  return cat1;
              }

              public void setCat1(String cat1) {
                  this.cat1 = cat1;
              }

              public int getUpdateCount() {
                  return updateCount;
              }

              public void setUpdateCount(int updateCount) {
                  this.updateCount = updateCount;
              }

              public String getPublishDate() {
                  return publishDate;
              }

              public void setPublishDate(String publishDate) {
                  this.publishDate = publishDate;
              }

              public String toString() {
                  return "Cat1UpdateCount [cat1=" + cat1 + ", updateCount=" + updateCount
                          + ", publishDate=" + publishDate + "]";
              }

          }

          參考:
          http://docs.spring.io/spring-data/data-mongodb/docs/current/reference/htmlsingle/#mongo.group

          http://docs.mongodb.org/manual/reference/operator/aggregation/group/
          http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

          posted on 2013-12-24 09:44 paulwong 閱讀(11659) 評論(0)  編輯  收藏 所屬分類: SPRINGMONGODB

          主站蜘蛛池模板: 琼结县| 哈尔滨市| 炎陵县| 武平县| 石首市| 宣城市| 东莞市| 鸡西市| 荔浦县| 商水县| 日喀则市| 阿图什市| 育儿| 永丰县| 江西省| 肇源县| 广饶县| 鹿泉市| 墨脱县| 四会市| 淄博市| 怀柔区| 台山市| 弋阳县| 石景山区| 利川市| 女性| 漳州市| 蓬安县| 顺义区| 曲阜市| 华亭县| 四会市| 平凉市| 全椒县| 孝感市| 彭州市| 简阳市| 仁寿县| 涟源市| 巴南区|