需要在 spring mongotemplate 中复制查询
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());
}
*我正在寻找是否可以使用 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());
}