How to order by included model property in LoopBack JS
Asked Answered
S

2

6

I have a person model which has a belongsTo relations with a meeting model. I´m doing the query

Person.find({include:['meetings']})

Which gives me a result like this one:

    person:{
        name:"person 1",
        age: 15
        meeting:{
            name: "The meeting",
            date:"June 26, 2019 11:13:00"
        }
    }

What I would like to do is to order the results of the find function by the meeting date. Is there any way I can achieve this on a single query?

I´ve tried this:

Person.find({include:['meeting'],order:"meeting.date DESC"})

But server crashed when trying this. Can anyone help me achieve this?

Skean answered 30/6, 2016 at 23:54 Comment(0)
S
1

Ordering can be done through model.json file as below:

{
    ...
    "scope": {
        "order": "properyName <ASC/DESC>"
    },
    ...
}

By default, ordering is in ascending order so no need to add ASC explicitly.

Stickleback answered 13/6, 2018 at 10:1 Comment(0)
T
0

Try this :

Person.find({
  include:{
    relation: 'meetings',
    scope: {
      order: 'date DESC'
    }
  }
});
Traceetracer answered 1/7, 2016 at 10:55 Comment(1)
Note that this will sort the Meetings inside each Person, not the Persons.Mortify

© 2022 - 2024 — McMap. All rights reserved.