如何在 spring 数据 mongo 数据库中使用聚合来使用 $month

how to use $month using aggregation in spring data mongo db

我正在使用 mongodb.I 必须在我的 spring 数据 mongo 数据库中对 $date 使用聚合查询。这是我的用户 collection.

{
    "_id" : NumberLong(70289),
    "_class" : "com.samepinch.domain.user.User",
    "age" : 25,
    "roles" : [
        "ROLE_MODERATOR",
        "ROLE_USER"
    ],
    "firstName" : "Abhi",
    "lastName" : "Saini",
    "email" : "abhisheksn138@gmail.com",
    "createdDate" : ISODate("2015-12-04T12:29:57.604Z"),
    "updatedDate" : ISODate("2016-02-10T06:23:13.314Z")
}

这是我的 mongodb 查询

 db.users.aggregate([{ $project : { month_joined : { $month : "$createdDate" } } } ,{ $group : { _id : {month_joined:"$month_joined"} , number : { $sum : 1 } } },{ $sort : { "_id.month_joined" : 1 } }])

查询给出了我想要的结果:

{ "_id" : { "month_joined" : 1 }, "number" : 19 }
{ "_id" : { "month_joined" : 2 }, "number" : 7 }
{ "_id" : { "month_joined" : 9 }, "number" : 16 }
{ "_id" : { "month_joined" : 10 }, "number" : 12 }
{ "_id" : { "month_joined" : 11 }, "number" : 11 }
{ "_id" : { "month_joined" : 12 }, "number" : 13 }

现在我必须使用 mongo 模板在 spring 数据 mongodb 中编写此查询。我刚开始使用 aggregation.Is 他们是使用它的任何简单方法。请帮忙

谢谢。

您可以尝试先在投影操作中使用SpEL andExpression投影字段,然后在分组操作中按新字段分组:

Aggregation agg = newAggregation(
    project("id").andExpression("month(createdDate)").as("month_joined"),
    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();

在上面,通过 newAggregation 静态工厂方法创建了一个新聚合,您将聚合操作列表传递给该方法。这些聚合操作定义了聚合的聚合管道。

在第一步中,您通过在项目操作中使用 SpEL andExpression 从输入集合中投影字段。

在第二步中,您使用组操作为每个 "month_joined" 值定义一个组,您通过 count() 聚合运算符聚合出现次数,并将结果收集到一个新的名为 "number".

的字段

作为第三步 select 字段 "number" 并为上一个组操作生成的 _id 字段创建别名(因此调用 previousOperation() ) 名称 "month_joined".

作为第四步,通过排序操作按出现次数升序对分组 month_joined 文档的结果列表进行排序。

最后在 MongoTemplate 上调用 aggregate() 方法,以便让 MongoDB 以创建的聚合作为参数执行实际的聚合操作。


要过滤当年进入管道的文档,您需要投影一个包含日期的年份部分的新字段,然后在项目步骤之后发出匹配运算符,例如

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();

从 2.1 开始,您可以使用 DateOperators.Month

Aggregation agg = newAggregation(
    project("id").and(DateOperators.Month.month("createdDate")).as("month_joined"),
    group("month_joined").count().as("number"),
    project("number").and("month_joined").previousOperation(),
    sort(ASC, "number")
);