Search code examples
javaspringmongodbspring-bootspring-repositories

Finding Max and Min values from a MongoDB Repository in Spring Boot


Is it possible to get the max and min values of a specific field within a repository within Spring Boot? If so, how do you do it?

I understand it is very easy using Mongo Shell.

https://docs.mongodb.com/manual/reference/operator/meta/max/

I've been investigating for a while now and still haven't come up with a solution.

Thanks in advance.


Solution

  • Found it.

    public interface OrderRepository extends MongoRepository<Order,String> {
    
    // Find MAX Value
    
    Order findTopByOrderByOrderDateDesc();
    
    // Find MIN Value
    
    Order findTopByOrderByOrderDateAsc();
    
    }