使用MongoTemplate转换mongo请求(spring)
我想转换此mongodb请求的一部分:使用MongoTemplate转换mongo请求(spring),spring,mongodb,mongotemplate,Spring,Mongodb,Mongotemplate,我想转换此mongodb请求的一部分: { "$group": { "_id": { "instrument_name": "$instrument_name", "interval":{ "$minute":{ $a
{
"$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")
);
你能帮我吗
谢谢您可以尝试先在投影操作中使用投影间隔字段,然后在分组操作中按新字段分组:
// 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")
);
您可以尝试先在投影操作中使用投影间隔字段,然后在分组操作中按新字段分组:
// 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")
);
我不知道我们是否可以优化这一点:
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")
);
TypedAggregation聚合=聚合.newAggregation(InstrumentModel.class,
聚合。匹配(标准,其中(“工具名称”)在(工具)中),
Aggregation.project()和(“仪器名称”)作为(“仪器名称”)和(“时间”)加上(历元).extractMinute()作为(“间隔”),
Aggregation.group(Fields.Fields()和(“仪器名称”)和(“间隔”).count()作为(“刻度”)
);
但是它是有效的,我不知道我们是否可以优化它:
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")
);
TypedAggregation聚合=聚合.newAggregation(InstrumentModel.class,
聚合。匹配(标准,其中(“工具名称”)在(工具)中),
Aggregation.project()和(“仪器名称”)作为(“仪器名称”)和(“时间”)加上(历元).extractMinute()作为(“间隔”),
Aggregation.group(Fields.Fields()和(“仪器名称”)和(“间隔”).count()作为(“刻度”)
);
但它是有效的