MongoMapper靠近maxDistance

MongoMapper靠近maxDistance

本文介绍了MongoMapper靠近maxDistance-Mongo :: OperationFailure:地理位置值必须是数字:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MongoMapper支持的模型中用maxDistance封装一个Near查询.

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.

型号

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英里范围内获得一切...

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

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

推荐答案

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

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

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

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

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

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

也请尝试Site.distinct(:location)并查找任何非数字数据. (如果您未使用MM 0.11.1,则为Site.query.distinct(:location).

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

提示:如果要查看查询命中MongoDB后的外观,请添加.criteria.to_hash:

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
  }
}

这篇关于MongoMapper靠近maxDistance-Mongo :: OperationFailure:地理位置值必须是数字:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:32