picardo
picardo

Reputation: 24886

How to make Mongoid order properly?

I've been trying to figure out why Mongoid thinks 387 is smaller than 67. I have two records and the row with 67 is appearing above the row with 387 every time. My query is simple

Building.desc(:violations)

I have also tried order_by() and it's the same story.

Edit:

I now changed the field type to Integer on some fields, and to BigDecimal for other fields where the numbers are greater -- especially currency figures. That fixed the Integer fields, but BigDecimal fields are still not sorting properly.

Upvotes: 3

Views: 3008

Answers (1)

Andy Lindeman
Andy Lindeman

Reputation: 12165

Check the data type on the fields. In MongoDB, it can be pretty easy to store something as a string when you really meant to store it as a numeric value of some sort.

More information and examples about data types available from the MongoDB documentation.

Upvotes: 4

Related Questions