Mongotemplate聚合java spring GroupBy

Posted

技术标签:

【中文标题】Mongotemplate聚合java spring GroupBy【英文标题】:Mongo Template Aggregtion java spring GroupBy 【发布时间】:2020-09-28 10:10:59 【问题描述】:
db.billingDomain.aggregate([
  
    $group: 
      _id: 
        day:  $dayOfMonth: "$createdAt" ,
        month:  $month: "$createdAt" ,
        year:  $year: "$createdAt" 
      ,
      count: 
        $sum: 1
      ,
      date: 
        $first: "$createdAt"
      
    
  ,
  
    $project: 
      date: 
        $dateToString: 
          format: "%Y-%m-%d",
          date: "$date"
        
      ,
      count: 1,
      _id: 0
    
  ,
  
    $match: 
      "date": 
        $gte: "2020-06-05"
      
    
  
])

如何在 Spring Java 中使用 mongo Template、Aggregation 构建此查询?进行此查询时面临挑战

【问题讨论】:

【参考方案1】:

Spring Mongo 并不支持所有的 MongoDB 语法,所以我们需要手动实现AggregationOperation(Java >=v1.8):

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.aggregation.DateOperators.*;


Aggregation agg = newAggregation(
    op -> new Document("$group",
        new Document("_id",
            new Document("day", new Document("$dayOfMonth", "$createdAt"))
                .append( "month", new Document("$month", "$createdAt"))
                .append( "year", new Document("$year", "$createdAt")))
        .append("count", new Document("$sum", 1))
        .append("date", new Document("$first", "$createdAt"))),
    project("count")
        .andExclude("_id")
        .and(dateOf("date").toString("%Y-%m-%d")).as("date"),
    match(where("date").gte("2020-06-05")));

AggregationResults<OutputClass> result = mongoTemplate.aggregate(agg,
    mongoTemplate.getCollectionName(BillingDomain.class), OutputClass.class);

编辑: System.out.println(agg);


   "aggregate":"__collection__",
   "pipeline":[
      
         "$group":
            "_id":
               "day":
                  "$dayOfMonth":"$createdAt"
               ,
               "month":
                  "$month":"$createdAt"
               ,
               "year":
                  "$year":"$createdAt"
               
            ,
            "count":
               "$sum":1
            ,
            "date":
               "$first":"$createdAt"
            
         
      ,
      
         "$project":
            "count":1,
            "_id":0,
            "date":
               "$dateToString":
                  "format":"%Y-%m-%d",
                  "date":"$date"
               
            
         
      ,
      
         "$match":
            "date":
               "$gte":"2020-06-05"
            
         
      
   ]

【讨论】:

项目方法在此方法中不可用 @ParminderSingh 你是什么意思? project("count")... @ParminderSingh 不清楚您的项目有什么问题?不行吗?

以上是关于Mongotemplate聚合java spring GroupBy的主要内容,如果未能解决你的问题,请参考以下文章

mongoTemplate聚合操作Demo

MongoTemplate 使用aggregate聚合查询

开发中使用mongoTemplate进行Aggregation聚合查询

java Mongondb聚合函数&去重

mongotemplate怎么获得统计数组的大小

怎么打印mongotemplate的日志