*I am looking to see if i can replicate this query using spring mongo template.
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' }
}])
If anyone could lead me in the right direction that would be great. Thanks!
Try this:
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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.