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?

Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74

2 Answers2

1

Try this :

Person.find({
  include:{
    relation: 'meetings',
    scope: {
      order: 'date DESC'
    }
  }
});
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
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.

20B2
  • 2,011
  • 17
  • 30