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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -