java - aggregate in Mongotemplate or get Max value -
i have make query have several users differents dates, , need extractr every user more recent date, achieve make works mongodb console, cannot found way extrapolate java spring using mongotemplate or mongorepository. more clear imagine have rows,
{userid:1, date:10} {userid:1, date:20} {userid:2, date:50} {userid:2, date:10} {userid:3, date:10} {userid:3, date:30}
i need receive list of:
{{userid:1, date:20}, {userid:2, date:50}, {userid:3, date:10} {userid:3, date:30}}
the aggregate Ï use one
db.table1.aggregate({$group:{'_id':'$userid', 'max':{$max:'$date'}}}, {$sort:{'max':1}}).result
regards.
you sort first date desc , select first while grouping userid
final aggregation aggregation = newaggregation( sort(desc, "date"), group("userid").first("date").as("date") ); final aggregationresults<user> results = mongotemplate.aggregate(aggregation, "user", user.class);
Comments
Post a Comment