1

我想转换这个 mongodb 请求的一部分:

{
        "$group": {
                "_id": {
                        "instrument_name": "$instrument_name",
                        "interval":{  
                             "$minute":{  
                                    $add:[new Date("1970-01-01"), "$date"]
                             }
                        }
                }
        }
}

使用 MongoTemplate 的聚合方法(使用 spring)。

我的问题是间隔属性。我想以一分钟的时间间隔返回一个仪器列表。

我尝试了许多解决方案,但我无法解决该部分:

                            "interval":{  
                             "$minute":{  
                                    $add:[new Date("1970-01-01"), "$date"]
                             }
                        }



Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(Criteria.where("instrument_name").in(instruments)),
        Aggregation.group("instrumentName", ???)),
        Aggregation.project().andExpression("instrumentName").as("instrument_name")
    );

你能帮助我吗?

谢谢

4

2 回答 2

0

您可以尝试先在投影操作中使用SpELandExpression来投影区间字段,然后在组操作中按新字段进行分组:

// define epochDate here e.g DateTime epochDate = ...
Aggregation agg = newAggregation(
    match(Criteria.where("instrument_name").in(instruments)),
    project("instrument_name").and("date").plus(epochDate).as("interval"), 
    group(fields().and("instrument_name").and("interval")).count().as("count")
);
于 2016-06-08T14:34:23.737 回答
0

我不知道我们是否可以优化这个:

TypedAggregation<InstrumentModel> aggregation = Aggregation.newAggregation(InstrumentModel.class,
        Aggregation.match(Criteria.where("instrument_name").in(instruments)),
        Aggregation.project().and("instrumentName").as("instrument_name").and("time").plus(epoch).extractMinute().as("interval"),
        Aggregation.group(Fields.fields().and("instrument_name").and("interval")).count().as("ticks")
    );

但它有效

于 2016-06-08T19:46:04.750 回答