sqlloopbackloopback4

Loopback 4 - How to find the highest value in column (Like SELECT MAX(column) from Table)?


I want to find the highest value in a specific column of a specific table. It should be very simple. this is the documentation of LB4 https://loopback.io/doc/en/lb2/Where-filter But I didn't find it there.


Solution

  • Within the Loopback Filter Documentation they do mention a way to achieve this, even though it's not as obvious.

    /weapons?filter[where][effectiveRange][gt]=900&filter[limit]=3
    

    Essentially you can do the following:

    1. Identify the column of interest.
    2. Use the gt operator to set a min number
    3. Add order if you wanted to ensure the sorting order is as expected.
    4. Limit the results to 1.

    Here is a code example:

    Employees.find({
      where: {
        age: {
          gt: 1
        }
      },
      order: 'age ASC',
      limit: 1
    })
    

    Please let me know if this is what you were going for or if you need some more support.