2012-01-08 151 views
0

所以我想在mongodb上使用runCommand来做geoNear,我希望结果按日期排序。MongoDB和Ruby:runCommand geoNear并按日期排序

我知道该怎么做的第一部分

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

但我如何得到结果,使得它被created_at有序?如果我是用宝石蒙哥要做到这一点,查询看起来像

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

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

回答

1

我不认为它可能以某种添加到geonear命令,按照链接

有效选项包括:“近”,“民”,“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)