Search code examples
mongodbmorphia

Get the maximum value of the field Morphia


I am trying to get the maximum/highest value available in my collections.

I am trying get the highest salary of the employee collection. We are using Morphia framework.

 Query<Employee> query = mongoDatastore.find(Employee.class).field("salary").....

//Get the maximum salary available in employee collection.

I am trying to get the maximum salary of the employee collection.

Can some one help me to how to get the highest salary of employee collection.


Solution

  • To get the highest salary of employee collection, use a combination of the order method to sort the collection by salary descending and limit method to return the top 1 document:

    Query<Employee> query = mongoDatastore.find(Employee.class).order("-salary").field("salary").limit(1)