我正在尝试在 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):