我试图在 MongoMapper 支持的模型中封装一个带有 maxDistance 的近查询。
我一定在我的查询语法中做了一些愚蠢的事情。
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
试图让所有东西都在一个点的 200 英里范围内......
站点.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):
你可能遇到过this bug https://jira.mongodb.org/browse/SERVER-1990这需要$near
and $maxDistance
订购时$maxDistance
first.
无论如何,我发现这个问题是因为我得到了OperationFailure: database error: geo values have to be number
使用 PyMongo 时,交换顺序修复了它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)