【发布时间】:2022-01-18 10:49:16
【问题描述】:
我一直在摸索如何以一种可以在应用程序中进一步使用的方式聚合数据。
我想做的是得到原始的
{
"_id" : ObjectId("61e0898effba6778d05827e0"),
"customId" : {
"_id" : UUID("315fa023-f6a4-4d34-865c-dbe661e46cd1")
},
"viewers" : 1,
"auditTime" : ISODate("2022-01-13T20:20:30.880Z"),
}
* lots
进入
{
"customId" : {
"_id" : UUID("315fa023-f6a4-4d34-865c-dbe661e46cd1")
},
"averageViewers" : 5,
"timePeriod" : ISODate("2022-01-13T20:10:00.00Z"),
}
我已经把时间段的匹配记录下来了
TypedAggregation<HistoricEntity> agg =
Aggregation.newAggregation(HistoricEntity.class,
Aggregation.match(Criteria.where("customId").is(channelId)),
Aggregation.match(Criteria.where("auditTime").gte(start).andOperator(Criteria.where("auditTime").lte(end))),
Aggregation.group("auditTime").avg("viewers").as("viewers")
);
AggregationResults<Map> aggregate = mongoTemplate.aggregate(agg, Map.class);
我认为理想的解决方案是使用 mongo atlas 聚合,我认为一旦解决这个问题就会很容易。
但我很茫然。任何提示或技巧将不胜感激
谢谢 亚当
【问题讨论】:
-
timePeriod是否总是准时(从您的问题标题)?我基于此发布了一个可能的答案
标签: spring mongodb spring-boot aggregate