MongoDB 和 Ruby:runCommand geoNear 并按日期排序
所以我想在 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 日期的顺序返回。有谁知道如何做到这一点?
So I want to do geoNear using runCommand on mongodb and I want the results to be sorted by date.
I know how to do the first part
db.users.runCommand({'geoNear' :"users",'near' : [-76.483999, 42.402794], 'spherical' : true, 'maxDistance' : 20/6378 })
but how do i get the results such that it is ordered by created_at? If I were to use Mongo gem to do this, the query would look like
User.database.command({'geoNear'=>"users",'near' => [-122, 37]}, 'spherical' => true, 'maxDistance' => 20/6378)
Still, I do not know how to sort it by date. I was considering of using an index on created_at in this case. I have indices on both location and created_at but the results are still not returned by order of created_at date. Does anyone have any clue on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为不可能向 geonear 命令添加排序,因为根据链接
默认情况下它按距离排序。
或者,您可以像这样编写球形查询,
您的 ruby mongmapper 等效项可能是(不确定,更好地验证)
I dont think its possible to add sort to geonear command, as per the link
by default it sorted by the distance.
Alternatively you can write your spherical query like this
your ruby mongomapper equivalent might be (not sure, better verify)