-1

I have list of documents on mongodb like

Id, name, surname, salary. 

I want all documents would return which contains max salary. I know that i should aggregate the query in two step.

  1. First of all, I should find max value of salar field for all documents,
  2. and after then i should find all documents where salary equals maxValue.

But i cannot configure it neither JPA nor native query approach. How can i make it happen?

Bago
  • 17
  • 7

1 Answers1

0
[{
 $group: {
  _id: '$salary',
  records: {
   $push: '$$ROOT'
  }
 }
}, {
 $sort: {
  _id: -1
 }
}, {
 $limit: 1
}]

Example of output

{
  "_id": 40,
  "records": [
    {
      "_id": {
        "$oid": "631b8ab07d8c3aa86bb1c5fc"
      },
      "name": "Bob",
      "surname": "B",
      "salary": 40
    },
    {
      "_id": {
        "$oid": "631b8ab07d8c3aa86bb1c5fd"
      },
      "name": "Alice",
      "surname": "A",
      "salary": 40
    }
  ]
}