有什么方法可以使用 mongoid 确保地理哈希顺序吗?

发布于 2024-12-22 14:27:22 字数 357 浏览 1 评论 0原文

有什么方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-12-29 14:27:22

为此,可以使用 mongoid 自定义字段序列化

这是一个很好的例子: https://github.com/ricodigo /shapado/blob/master/app/models/geo_position.rb

这是我自己的实现,它将 mongodb 中的位置存储为数组:

class LatLng
  include Mongoid::Fields::Serializable

  attr_reader :lat, :lng

  def serialize(value)
    return if value.nil?

    if value.is_a?(self.class)
      [value.lng.to_f, value.lat.to_f]
    elsif value.is_a?(::Hash)
      hash = value.with_indifferent_access
      [hash['lng'].to_f, hash['lat'].to_f]
    end
  end

  def deserialize(value)
    return if value.nil?

    value.is_a?(self.class) ? value : LatLng.new(value[1], value[0])
  end

  def initialize(lat, lng)
    @lat, @lng = lat.to_f, lng.to_f
  end

  def [](arg)
    case arg
      when "lat"
        @lat
      when "lng"
        @lng
    end
  end

  def to_a
    [lng, lat]
  end

  def ==(other)
    other.is_a?(self.class) && other.lat == lat && other.lng == lng
  end
end

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:

class LatLng
  include Mongoid::Fields::Serializable

  attr_reader :lat, :lng

  def serialize(value)
    return if value.nil?

    if value.is_a?(self.class)
      [value.lng.to_f, value.lat.to_f]
    elsif value.is_a?(::Hash)
      hash = value.with_indifferent_access
      [hash['lng'].to_f, hash['lat'].to_f]
    end
  end

  def deserialize(value)
    return if value.nil?

    value.is_a?(self.class) ? value : LatLng.new(value[1], value[0])
  end

  def initialize(lat, lng)
    @lat, @lng = lat.to_f, lng.to_f
  end

  def [](arg)
    case arg
      when "lat"
        @lat
      when "lng"
        @lng
    end
  end

  def to_a
    [lng, lat]
  end

  def ==(other)
    other.is_a?(self.class) && other.lat == lat && other.lng == lng
  end
end
冷情 2024-12-29 14:27:22

使用 BSON::OrderedHash

BSON::OrderedHash[:lng => 33.33, :lat => 45]

Use BSON::OrderedHash

BSON::OrderedHash[:lng => 33.33, :lat => 45]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文