Is there any way to ensure geo hash order using mongoid?
Currently I store it this way and do some magic in callback to ensure order:
field :location, type: Hash // { :lng => 33.33, :lat => 45 }
set_callback(:save, :before) do |doc|
if doc.location_changed?
doc.location = {lng: doc.location[:lng], lat: doc.location[:lat]}
end
end
May be there is some way to declare this Hash as class. I thought about Embeded Document, but it has _id.
It is possible to use mongoid custom field serialization for this.
Here is a good example: https://github.com/ricodigo/shapado/blob/master/app/models/geo_position.rb
Here is my own implementation that stores location in mongodb as array: