Ordering and grouping in mangoids

I need groups of people that are ordered by age

I tried something like this

Person.only(:name).asc(:age).group 

But he did not order by age.

How can i do this?

I am using mongoid beta 19.

+4
source share
1 answer

If I remember correctly, as soon as you called

 Person.only(:name) 

Mongoid basically considers the collection only for this field, therefore, as soon as you try to sort by age, there is no age field to sort, so it does nothing.

Try

 Person.asc(:age).only(:name).group 

Or maybe

 Person.asc(:age).only([:name, :age]).group 
+2
source

Source: https://habr.com/ru/post/1336091/


All Articles