[英]Need to replicate query in spring mongotemplate
*我想看看是否可以使用 spring mongo 模板復制此查詢。
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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.