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

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -