Spring mongoDB日期分钟子集上的聚合组

Spring mongoDB日期分钟子集上的聚合组,spring,mongodb,datetime,group-by,aggregation,Spring,Mongodb,Datetime,Group By,Aggregation,我正在使用Spring数据mongodb运行Spring 2.2.7 我有一个名为BaseSample的实体存储在samplesMongoDb集合中,我希望从创建日期开始按分钟对记录进行分组,并获取收集的平均值。我不知道如何在组聚合操作中使用DateOperators.Minute 详细说明 @Data @Document(collection = "samples") @EqualsAndHashCode public class BaseSample extends Message {

我正在使用Spring数据mongodb运行Spring 2.2.7

我有一个名为BaseSample的实体存储在samplesMongoDb集合中,我希望从创建日期开始按分钟对记录进行分组,并获取收集的平均值。我不知道如何在组聚合操作中使用DateOperators.Minute

详细说明

@Data
@Document(collection = "samples")
@EqualsAndHashCode
public class BaseSample extends Message {

    private float value;
    private UdcUnitEnum unit;
    private float accuracy;
    public LocalDateTime recorded;

}
它扩展了消息

@Data
@EqualsAndHashCode
public class Message {

    @Indexed
    private String sensorUuId;
    @Indexed
    private String fieldUuId;
    @Indexed
    private String baseStationUuId;
    @Indexed
    private Date created;

}
我想对其应用此查询

db.samples.aggregate ([
    {
        $match: {
            $and : [ {fieldUuId:"BS1F1"}, {unit: "DGC"}, {created : {$gte : ISODate("2020-05-30T17:00:00.0Z")}}, {created : {$lt : ISODate("2020-05-30T17:15:00.0Z")}}]
            }
    },
    {
        $group: {
        _id : { $minute : "$created"},
        date : {$first : {$dateToString:{date: "$created", format:"%Y-%m-%d"}}},        
        time : {$first : {$dateToString:{date: "$created", format:"%H:%M"}}},
        unit : {$first : "$unit"},
        data : { $avg : "$value"}
        }

    },
    {
    $sort: {date:1, time:1}
    }
])
关于样本采集(exerpt)

我对以下方法进行编码,以获得选定字段(传感器逻辑组)中选定单元类型(度,…)每分钟分组的样本平均值

public List aggregateFromField(字符串fieldUuId、udcUnit、LocalDateTime from、LocalDateTime to、可选页码、可选页面大小){

在agregation组操作中使用DateOperators.Minute类型有什么想法吗


提前下载。

您不能使用过时的
SimpleDateFormat
格式来格式化现代
LocalDateTime
。您需要使用
DateTimeFormatter
。无论使用其中一种格式还是另一种格式,请注意格式格式字母的情况,
YYYY
yyyyy
并不意味着相同的事情,也不会产生任何影响总是会产生相同的结果。你是对的,实际上我昨天更改了代码。现在aggregateFromField方法在from/to DateMatch操作中使用LocalDateTime对象,无需解析为字符串即可正常工作(签入测试).gateway项目中的控制器调用服务use private final DateTimeFormatter dtf=DateTimeFormatter.of模式(“u-M-d H:M”);以提供输入LocalDateTime对象。感谢您的评论@OleYou不能使用过时的
SimpleDateFormat
来格式化现代
LocalDateTime
。您需要使用
DateTimeFormatter
。无论使用其中一个,都要注意格式模式字母、
YYYY
yyyyyy的情况不是同一个意思,也不会总是产生相同的结果。你是对的,事实上我昨天更改了代码。现在aggregateFromField方法在from/to DateMatch操作中使用LocalDateTime对象,无需解析为字符串即可正常工作(签入测试).gateway项目中的控制器调用该服务,使用私有的final DateTimeFormatter dtf=DateTimeFormatter.of模式(“u-M-d H:M”);来提供输入LocalDateTime对象。谢谢您的评论@Ole
{
    "_id" : ObjectId("5ed296150af58a1c60c4f154"),
    "value" : 90.85242462158203,
    "unit" : "HPA",
    "accuracy" : 0.6498473286628723,
    "recorded" : ISODate("2020-05-30T17:21:25.850Z"),
    "sensorUuId" : "458f0ffd-13f9-466d-81a1-8d2e1c808da9",
    "fieldUuId" : "BS1F2",
    "baseStationUuId" : "BS1",
    "created" : ISODate("2020-05-30T17:21:25.777Z"),
    "_class" : "org.open_si.udc_common.models.BaseSample"
}
{
    "_id" : ObjectId("5ed296150af58a1c60c4f155"),
    "value" : 40.84038162231445,
    "unit" : "HPA",
    "accuracy" : 0.030185099691152573,
    "recorded" : ISODate("2020-05-30T17:21:25.950Z"),
    "sensorUuId" : "b396264f-fcd5-4653-8ac8-358ca3a4cb87",
    "fieldUuId" : "BS2F3",
    "baseStationUuId" : "BS2",
    "created" : ISODate("2020-05-30T17:21:25.868Z"),
    "_class" : "org.open_si.udc_common.models.BaseSample"
}
Pageable paging = new PagingHelper(pageNumber, pageSize).getPaging();

MatchOperation fieldMatch = Aggregation.match(Criteria.where("fieldUuId").is(fieldUuId));
MatchOperation unitMatch = Aggregation.match(Criteria.where("unit").is(unit.name()));
MatchOperation fromDateMatch = Aggregation.match(Criteria.where("created").gte(from));
MatchOperation toDateMatch = Aggregation.match(Criteria.where("created").lt(to));

DateOperators.Minute minute = DateOperators.Minute.minuteOf("created");

GroupOperation group = Aggregation.group("created")
        .first("created").as("date")
        .first("created").as("time")
        .first("unit").as("unit")
        .avg("value").as("avg")
        ;

SortOperation sort = Aggregation.sort(Sort.by(Sort.Direction.ASC, "$date")).and(Sort.by(Sort.Direction.ASC, "$time"));
SkipOperation skip = Aggregation.skip(paging.getOffset());
LimitOperation limit = Aggregation.limit(paging.getPageSize());

Aggregation agg = Aggregation.newAggregation(
        fieldMatch,
        unitMatch,
        fromDateMatch,
        toDateMatch,
        group,
        sort,
        skip,
        limit
);


AggregationResults<SampleAggregationResult> results = mongoTemplate.aggregate(agg, mongoTemplate.getCollectionName(BaseSample.class), SampleAggregationResult.class);

return results.getMappedResults();
@Data
public class SampleAggregationResult {

    private String date;
    private String time;
    private String unit;
    private float data;

}