没有ORM的MongoDB Ruby Bounds查询

时间:2012-04-24 01:25:34

标签: ruby mongodb geospatial

我正在试图弄清楚如何使用ruby驱动程序向mongodb发出一个边界框地理查询,而不使用像Mongoid或MongoMapper这样的ORM包装器。

我可以执行near命令没问题,但我似乎无法弄清楚内部的查找语法。

如果我想在半径范围内查询

,那么这就像魅力一样
conn = Mongo::Connection.from_uri('my DB')
  db = conn.db('my_sites')
  coll = db.command({'geoNear' => "sites",
             'near'=>[lng,lat],
             'spherical' => true,
             'maxDistance' => distance_in_radians,
             'num' => limit})
  render :json => coll['results'].to_a

但是我很难在内部获取查询:

box = [[34.05,-118.24],[35.80,116.44]]
coll = db.command({'within' => "sites", 'box' => box}

db['my_sites']
coll = db.find({"box" => box})

我可以直接在mongo客户端发出查询,但我刚刚了解了ruby驱动程序语法。

1 个答案:

答案 0 :(得分:0)

AFAIK,MongoDB shell版本如下所示:

db.my_sites.find(loc: { $within: { $box: box } })

低级Ruby接口通常镜像JavaScript接口,所以我认为你想要:

db['my_sites'].find(:loc => { :$within => { :$box => box } })

我没有方便的地理位置收藏,所以我无法测试它以确保。