我想在MongoMapper支持的模型中封装一个近距离查询与maxDistance。MongoMapper与maxDistance附近 - Mongo :: OperationFailure:地理值必须是数字:
我在查询语法中必须做些傻事。
型号
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):
此修复程序也适用于我。谢谢你为我节省了一些时间。 – Alice 2013-07-23 15:53:03