MongoMapper near with maxDistance - Mongo::OperationFailure: geo values have to be numbers:

自古美人都是妖i 提交于 2019-12-12 12:13:03

问题


I'm trying to encapsulate a near query with a maxDistance in a MongoMapper backed model.

I must be doing something silly in my query syntax.

Model

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

Trying to get everything within 200 miles of a point...

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):

回答1:


I'm guessing it's actually a data problem or index problem, your query looks correct.

See http://www.mongodb.org/display/DOCS/Geospatial+Indexing ... MaxDistance of 200 may be too large:

By default, the index assumes you are indexing longitude/latitude and is thus configured for a [-180..180) value range.

The distance unit is the same as in your coordinate system.

Also try Site.distinct(:location) and look for any non-numeric data. (or Site.query.distinct(:location) if you're not on MM 0.11.1).

Hint: If you want to see what a query is going to look like when it hits MongoDB, add .criteria.to_hash:

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



回答2:


You may have run into this bug that requires $near and $maxDistance to be ordered with $maxDistance first.

In any case, I found this question because I was getting OperationFailure: database error: geo values have to be number when using PyMongo, and swapping the order fixed it.




回答3:


Here's another solution for this one

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



回答4:


got this error with datas stored in session ( different contest, php + zf2) : a floatval(lat), floatval(lon) solved the issue :) I'm posting it in case somebody needs it :)



来源:https://stackoverflow.com/questions/10020755/mongomapper-near-with-maxdistance-mongooperationfailure-geo-values-have-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!