如何使用$月份春季数据MongoDB中使用聚合

问题描述 投票:3回答:2

我使用mongodb.I必须使用$日聚集在查询春天我蒙戈数据DB。这是我的用户集合。

{
    "_id" : NumberLong(70289),
    "_class" : "com.samepinch.domain.user.User",
    "age" : 25,
    "roles" : [
        "ROLE_MODERATOR",
        "ROLE_USER"
    ],
    "firstName" : "Abhi",
    "lastName" : "Saini",
    "email" : "[email protected]",
    "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 }

现在,我必须写使用mongotemplate春天的mongodb数据这个查询。我在使用aggregation.Is他们是使用它没有简单的方法新。请帮忙

谢谢。

java mongodb spring-mvc spring-data-mongodb
2个回答
2
投票

你可以尝试通过在集团化运作的新领域中的投影操作的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"值。

作为第三步骤选择字段"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();

1
投票

由于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")
);
© www.soinside.com 2019 - 2024. All rights reserved.