I need to sort the query by the average of two attributes, something like Story.scoped.order_by('(importance + points)/2').
As I saw on MongoDB and Origin documentation, this don’t seems to be possible. Do I need to create a third attribute with the average result and order by it?
| Story | points | importance
| first expected | 1 | 1
| third expected | 5 | 1
| second expected | 1 | 3
You can use the aggregation framework to do this. I don’t know Ruby/mongoid, but here’s how you’d do it in JavaScript:
Outputs: