MongoDB 和 Ruby:runCommand geoNear 并按日期排序

发布于 2024-12-25 09:32:11 字数 526 浏览 1 评论 0原文

所以我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2025-01-01 09:32:11

我认为不可能向 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​​ mongmapper 等效项可能是(不确定,更好地验证)

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

I dont think its possible to add sort to geonear command, as per the link

Valid options are: "near", "num", "maxDistance", "distanceMultiplier"
and "query".

by default it sorted by the distance.

Alternatively you can write your spherical query like this

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

your ruby mongomapper equivalent might be (not sure, better verify)

Users.where(:loc => {'$nearSphere' => [-122, 37],'$maxDistance'=>20/6378 }).sort(:created_at.desc)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文