MongoTemplate聚合

编程入门 行业动态 更新时间:2024-10-28 04:29:41
本文介绍了MongoTemplate聚合 - 按日期分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用mongotemplate创建聚合查询,其中按日期分组(即2016-03-01)而不是datetime(即2016-03-01 16:40:12)。 mongodb文档中存在 dateToString 操作,它可用于使用格式化从日期时间中提取日期: docs.mongodb/manual/reference/operator/aggregation/dateToString/ 但我得到它与mongotemplate一起工作 - 我得到一个NullPointerException。 (我的db版本是3.2)

I'm trying to create an aggregate query using mongotemplate where there's a grouping by date (i.e 2016-03-01) instead of datetime (i.e 2016-03-01 16:40:12). The dateToString operation exists in the mongodb documentation it can be used to extract the date from the datetime using formatting: docs.mongodb/manual/reference/operator/aggregation/dateToString/ but I get get it to work with mongotemplate - I get a NullPointerException. (my db version is 3.2)

List<AggregationOperation> aggregationOperations = new ArrayList<AggregationOperation>(); aggregationOperations.add( Aggregation.project("blabla", ...). andExpression("dateToString('%Y-%m-%d',timeCreated).as("date")); aggregationOperations.add(Aggregation.group("date").sum("blabla").as("blabla")); AggregationResults<?> aggregationResults = this.mongoTemplate.aggregate( Aggregation.newAggregation(aggregationOperations), collectionName, resultClass);

当我使用 dayOfMonth(timeCreated)来提取日期时,没有例外,但是我找不到如何使用 dateToString 进行此操作的示例。我没有使用''作为日期格式,它也没有用...

When I use dayOfMonth(timeCreated) to extract the day, there's no exception, but I couldn't find and example of how to make this work with dateToString. I tried without '' for the date format, and it also didn't work...

这是我得到的例外:

java.lang.NullPointerException at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:226) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255) at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:324) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:263) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:136) at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:36) at com.mongodb.OutMessage.putObject(OutMessage.java:289) at com.mongodb.OutMessage.writeQuery(OutMessage.java:211) at com.mongodb.OutMessage.query(OutMessage.java:86) at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:81) at com.mongodb.DBmand(DB.java:320) at com.mongodb.DBmand(DB.java:299) at com.mongodb.DBmand(DB.java:374) at com.mongodb.DBmand(DB.java:246) at org.springframework.data.mongodb.core.MongoTemplate$2.doInDB(MongoTemplate.java:357) at org.springframework.data.mongodb.core.MongoTemplate$2.doInDB(MongoTemplate.java:355) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:442) at org.springframework.data.mongodb.core.MongoTemplate.executeCommand(MongoTemplate.java:355) at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1497) at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1432)

编辑:

最终我们在这里决定采用与下面建议不同的解决方案,我在这里写,以防其他人认为它有用:

Eventually we decided here on a different solution than what was suggested below, I'm writing it here in case anyone else finds it useful:

除了保存日期时间的timeCreated字段,我们在文档中保存了另一个字段:date,它只保存日期(长)。

In addition to the "timeCreated" field which holds the datetime, we saved another field in the document: "date", that holds just the date (as long).

例如,如果 timeCreated=2015-12-24 16:36:06.657 + 02:00,然后日期是2015-12-24 00:00:00,我们节省了1449180000000. 现在我们可以简单地分组通过date。

For example if "timeCreated" = "2015-12-24 16:36:06.657+02:00", then date is "2015-12-24 00:00:00", and we save 1449180000000. Now we can simply group by "date".

推荐答案

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

You could try projecting the fields first by using the SpEL andExpression in the projection operation and then group by the new fields in the group operation:

Aggregation agg = newAggregation( project() .andExpression("year(timeCreated)").as("year") .andExpression("month(timeCreated)").as("month") .andExpression("dayOfMonth(timeCreated)").as("day"), group(fields().and("year").and("month").and("day")) .sum("blabla").as("blabla") ); AggregationResults<BlaBlaModel> result = mongoTemplate.aggregate(agg, collectionName, BlaBlaModel.class); List<BlaBlaModel> resultList = result.getMappedResults();

更多推荐

MongoTemplate聚合

本文发布于:2023-11-22 00:40:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MongoTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!