【问题标题】:Mongo Template Aggregtion java spring GroupByMongotemplate聚合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 构建此查询?进行此查询时面临挑战

【问题讨论】:

    标签: mongodb spring-boot aggregation-framework spring-mongodb mongotemplate


    【解决方案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 不清楚您的项目有什么问题?不行吗?
    猜你喜欢
    • 1970-01-01
    • 2019-10-14
    • 2023-03-20
    • 2019-04-12
    • 1970-01-01
    • 2023-02-13
    • 2015-01-08
    • 2016-04-07
    • 2019-06-28
    相关资源
    最近更新 更多