Java 需要在spring mongotemplate中复制查询
*我想看看是否可以使用SpringMongo模板复制这个查询Java 需要在spring mongotemplate中复制查询,java,spring,mongodb,spring-data-mongodb,Java,Spring,Mongodb,Spring Data Mongodb,*我想看看是否可以使用SpringMongo模板复制这个查询 db.getCollection('historical-hourly').aggregate([ {$match: { "city": "london", "hourlyWeather.time": { $gte: 1582779600 } } },{ $sort: { "createdDateTime": -1 }},{ $group: { _id:{
db.getCollection('historical-hourly').aggregate([
{$match: {
"city": "london",
"hourlyWeather.time": {
$gte: 1582779600
}
}
},{ $sort: { "createdDateTime": -1 }},{
$group: {
_id:{
"city": "$city",
"time": "$hourlyWeather.time"
},
'doc': { '$first': '$$ROOT' }
}
},
{
'$replaceRoot': { 'newRoot': '$doc' }
}])
如果有人能把我引向正确的方向,那就太好了。谢谢 试试这个:
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
Aggregation agg = Aggregation.newAggregation(
match(Criteria.where("city").is("london").and("hourlyWeather.time").gte(1582779600)),
sort(Direction.DESC, "createdDateTime"),
group(Fields.from(Fields.field("city"), Fields.field("time", "hourlyWeather.time"))).first("$$ROOT").as("doc"),
replaceRoot("doc")
);
AggregationResults<Document> docs = mongoTemplate.aggregate(agg, "historical-hourly", Document.class);
for(Document doc: docs) {
System.out.println(doc.toJson());
}
import static org.springframework.data.mongodb.core.aggregation.aggregation.*;
聚合agg=Aggregation.newAggregation(
匹配(标准。其中(“城市”)为(“伦敦”)和(“小时天气时间”).gte(1582779600)),
排序(Direction.DESC,“createdDateTime”),
组(Fields.from(Fields.field(“city”)、Fields.field(“time”、“hourlyWeather.time”))。首先($$ROOT”)。作为(“doc”),
replaceRoot(“doc”)
);
AggregationResults docs=mongoTemplate.aggregate(agg,“历史小时”,Document.class);
用于(文档:文档){
System.out.println(doc.toJson());
}
效果很好!谢谢,这正是我想要的。