Zaif
Zaif

Reputation: 1

How to get the maximum value of a field by some conditions in mongodb

I am new to mongodb, I am trying to write a query for finding max salary by department equal to IT.How can i write the query in mongobd i know how to write this query in oracle but i am unable to write query in mongodb.I have check other question on stack overflow but not found any related to my question.If someone knows please guide me.Thank you. my collection:

{
"salary" : "1000",
"department" : "IT"
}
{
"salary" : "2000",
"department" : "IT"
}
{
"salary" : "3000",
"department" : "IT"
}

expected out put:

{
"salary" : "3000"
}

i know this is very silly question but i am new here for mongodb.

Upvotes: 0

Views: 416

Answers (1)

Eugen
Eugen

Reputation: 917

Query query = new Query();
query.with(new Sort(Sort.Direction.DESC, "salary"));
query.limit(1);
QueryObject maxObject = mongoTemplate.findOne(query, QueryObject.class);

Upvotes: 1

Related Questions