所以我想在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 date的顺序返回。有没有人知道如何做到这一点?
答案 0 :(得分: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
你的红宝石mongomapper等价物可能是(不确定,更好的验证)
Users.where(:loc => {'$nearSphere' => [-122, 37],'$maxDistance'=>20/6378 }).sort(:created_at.desc)