How to sort by one of two fields using Mongoid order_by (not Aggregation Pipeline)?

Viewed 14

Given a document:

field :hsh, type: Hash

hsh can have one of two keys. Either one will always be present, and not both.

{ quality_v1: 5 }

or

{ quality_v2: 7 }

How to sort using Mongoid order_by (not Aggregation Pipeline) so that documents will be sorted by either quality_v1 or quality_v2, whichever is present?

For example:

{ quality_v1: 7 }
{ quality_v2: 7 }
{ quality_v1: 6 }
{ quality_v2: 5 }
0 Answers
Related