【问题标题】:MongoMapper near with maxDistance - Mongo::OperationFailure: geo values have to be numbers:MongoMapper 靠近 maxDistance - Mongo::OperationFailure:地理值必须是数字:
【发布时间】:2012-04-04 23:36:10
【问题描述】:

我正在尝试在 MongoMapper 支持的模型中封装具有 maxDistance 的近查询。

我的查询语法一定是在做一些愚蠢的事情。

型号

class Site
  include MongoMapper::Document

  key :id, Integer 
  key :name, String
  key :location, Array
  ensure_index [[:location, '2d']]


  def self.nearest(center_point, range)
    where(:location => {'$near' => center_point, '$maxDistance' => range}).all
  end

end

试图在一个点的 200 英里范围内获得所有东西......

Site.nearest([-122.0,44.0],200)

> Mongo::OperationFailure: geo values have to be numbers: {
> $maxDistance: 200, $near: [ -122.0, 44.0 ] }  from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:144:in
> `next'    from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:290:in
> `each'    from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:308:in
> `to_a'    from
> /Library/Ruby/Gems/1.8/gems/mongo-1.6.1/lib/mongo/cursor.rb:308:in
> `to_a'    from
> /Library/Ruby/Gems/1.8/gems/plucky-0.4.4/lib/plucky/query.rb:74:in
> `all'     from /Users/nick/Code/web/map/app/models/site.rb:40:in
> `nearest'     from (irb):

【问题讨论】:

    标签: mongodb geospatial mongomapper


    【解决方案1】:

    您可能遇到过this bug,这需要先使用$maxDistance 订购$near$maxDistance

    无论如何,我发现这个问题是因为我在使用 PyMongo 时收到了OperationFailure: database error: geo values have to be number,并且交换了这个问题。

    【讨论】:

    • 这个修复也对我有用。感谢您为我节省了一些时间。
    【解决方案2】:

    我猜这实际上是数据问题或索引问题,您的查询看起来正确。

    http://www.mongodb.org/display/DOCS/Geospatial+Indexing ... MaxDistance 200 可能太大了:

    默认情况下,索引假定您正在索引经度/纬度,因此配置为 [-180..180) 值范围。

    距离单位与您的坐标系中的相同。

    也可以尝试Site.distinct(:location) 并查找任何非数字数据。 (或Site.query.distinct(:location),如果您不在 MM 0.11.1 上)。

    提示:如果您想查看查询到达 MongoDB 时的样子,请添加 .criteria.to_hash

    Site.where(:location => {'$near' => center_point, '$maxDistance' => range}).criteria.to_hash
    # =>
    {
      :location=> {
        "$near"        => [-122.0, 44.0],
        "$maxDistance" => 200
      }
    }
    

    【讨论】:

      【解决方案3】:

      这是另一个解决方案for this one

      {'location' : SON([('$near', [55.55632, 25.13522]), ('$maxDistance', 0.01)])})
      

      【讨论】:

        【解决方案4】:

        存储在会话中的数据出现此错误(不同的比赛,php + zf2):floatval(lat),floatval(lon)解决了这个问题:) 我发布它以防有人需要它:)

        【讨论】:

          猜你喜欢
          • 2018-06-08
          • 2021-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-07
          相关资源
          最近更新 更多