Max value from field Spring Data and Mongodb -
i want calculate maximum value of field code within entity "user", use spring data, have seen code as:
".find({}).sort({"updatetime" : -1}).limit(1)"
but not know how integrate "repository" @query
or equivalent solution, return maximum value of said field. can me? thank you.
you can write custom method repository. example have:
public interface userrepository extends mongorepository<user, string>, userrepositorycustom { ... }
additional methods repository:
public interface userrepositorycustom { user maxuser(); }
and implementation of it:
public class userrepositoryimpl implements userrepositorycustom { @autowired private mongotemplate mongotemplate; @override public user maxuser() { final query query = new query() .limit(1) .with(new sort(sort.direction.desc, "updatetime")); return mongotemplate.findone(query, user.class) } }
Comments
Post a Comment