paulwong

          在SPRING DATA MONGODB中使用聚合統(tǒng)計查詢

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


          在MONGODB中的架構(gòu)圖



          在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關(guān)系的表,需同時JOIN這兩張表的,展開子項LIST,且是內(nèi)鏈接,即如果父和子的關(guān)聯(lián)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()//為前一操作所產(chǎn)生的ID FIELD建立別名
                      );

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

          }

          其中frags的數(shù)據(jù)類型是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

          主站蜘蛛池模板: 恩施市| 邯郸县| 正宁县| 马公市| 平和县| 中方县| 襄城县| 凤山市| 镇沅| 遂平县| 白玉县| 宜兰市| 崇州市| 鹿泉市| 长沙市| 迭部县| 象山县| 盐津县| 安塞县| 唐山市| 淮北市| 东至县| 开江县| 兴文县| 剑川县| 邢台市| 祁连县| 水富县| 神农架林区| 阳高县| 景东| 西畴县| 来凤县| 鹰潭市| 崇文区| 监利县| 河西区| 许昌县| 灵石县| 贵溪市| 大竹县|