有什么方法可以使用 mongoid 确保地理哈希顺序吗?
有什么方法可以使用 mongoid 确保地理哈希顺序吗?
目前我以这种方式存储它,并在回调中执行一些魔法以确保顺序:
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
可能有某种方法可以将此哈希声明为类。我考虑过嵌入文档,但它有_id。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,可以使用 mongoid 自定义字段序列化。
这是一个很好的例子: https://github.com/ricodigo /shapado/blob/master/app/models/geo_position.rb
这是我自己的实现,它将 mongodb 中的位置存储为数组:
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:
使用 BSON::OrderedHash
Use
BSON::OrderedHash