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 閱讀(11662) 評論(0)  編輯  收藏 所屬分類: SPRINGMONGODB

          主站蜘蛛池模板: 永和县| 安庆市| 兰溪市| 任丘市| 涿州市| 翼城县| 全州县| 华宁县| 金华市| 峨山| 偃师市| 烟台市| 大丰市| 开封县| 福鼎市| 扬州市| 聊城市| 锡林郭勒盟| 白山市| 南陵县| 乐清市| 依安县| 怀宁县| 葵青区| 香港| 丰原市| 新晃| 石狮市| 积石山| 巴中市| 繁峙县| 红原县| 长治市| 鹰潭市| 西充县| 武山县| 吐鲁番市| 崇仁县| 宣化县| 通州市| 东丰县|