paulwong

spring data mongodb group by month

https://stackoverflow.com/questions/35312677/how-to-use-month-using-aggregation-in-spring-data-mongo-db
Aggregation agg = newAggregation(
    project("id")
        .andExpression("month(createdDate)").as("month_joined")
        .andExpression("year(createdDate)").as("year"),
    match(Criteria.where("year").is(2016)),
    group("month_joined").count().as("number"),
    project("number").and("month_joined").previousOperation(),
    sort(ASC, "number")
);

AggregationResults<JoinCount> results = mongoTemplate.aggregate(agg, 
                                                     "collectionName", JoinCount.class);
List<JoinCount> joinCount = results.getMappedResults();

posted on 2020-12-11 16:53 paulwong 阅读(214) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: