【问题标题】:MongoDB Ruby Bounds Query without an ORM没有 ORM 的 MongoDB Ruby 边界查询
【发布时间】:2012-04-24 01:25:33
【问题描述】:

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

我可以执行附近的命令没问题,但我似乎无法弄清楚带有 inside 的 find 的语法。

所以如果我想在半径范围内查询,这就像一个魅力

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​​ 驱动程序语法时被绊倒了。

【问题讨论】:

    标签: ruby mongodb geospatial


    【解决方案1】:

    AFAIK,MongoDB shell 版本如下所示:

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

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

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

    我手边没有适合地理环境的集合,因此无法对其进行测试以确保。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-12
      • 2019-05-11
      • 2012-11-13
      • 2017-09-29
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多