【问题标题】:MongoDB and Ruby: runCommand geoNear and sorting by dateMongoDB 和 Ruby:runCommand geoNear 和按日期排序
【发布时间】:2012-01-08 02:17:12
【问题描述】:

所以我想在 mongodb 上使用 runCommand 执行 geoNear,我希望结果按日期排序。

我知道如何做第一部分

db.users.runCommand({'geoNear' :"users",'near' : [-76.483999, 42.402794], 'spherical' : true, 'maxDistance' : 20/6378 })

但是我如何获得由 created_at 排序的结果?如果我要使用 Mongo gem 来执行此操作,查询将如下所示

User.database.command({'geoNear'=>"users",'near' => [-122, 37]}, 'spherical' => true, 'maxDistance' => 20/6378)

不过,我不知道如何按日期对其进行排序。在这种情况下,我正在考虑在 created_at 上使用索引。我在 location 和 created_at 上都有索引,但结果仍然没有按 created_at 日期的顺序返回。有没有人知道如何做到这一点?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 mongodb mongomapper


    【解决方案1】:

    我认为不可能根据链接向geonear 命令添加排序

    有效选项有:“near”、“num”、“maxDistance”、“distanceMultiplier” 和“查询”。

    默认按距离排序。

    您也可以像这样编写球形查询

    db.users.find( { loc :  { $nearSphere : [80.21223299999997, 13.034892],$maxDistance:20/6378  } } ).sort({ created_at : -1 } ) //-1 for descending
    

    您的 ruby​​ mongomapper 等效项可能是(不确定,更好地验证)

    Users.where(:loc => {'$nearSphere' => [-122, 37],'$maxDistance'=>20/6378 }).sort(:created_at.desc)
    

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 2015-10-07
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多