简体   繁体   English

MongoDB GeoNear的结果除了距离以外的其他东西?

[英]Sorting MongoDB GeoNear results by something other than distance?

I'm developing a PHP application in which we need to retrieve results within a certain boundary, but ordered by the create date of the results, not the distance. 我正在开发一个PHP应用程序,我们需要在某个边界内检索结果,但是按结果的创建日期排序,而不是距离。 I figured MongoDB's geoNear command would be great for this, since it takes care of calculating the distance for each result. 我认为MongoDB的geoNear命令对此很有用,因为它负责计算每个结果的距离。 However, I was wondering if there was a way to specify sorting by the create_date attribute, rather than distance. 但是,我想知道是否有办法通过create_date属性指定排序,而不是距离。 Ideally I would create a compound key index of coordinates and create date, and then quickly retrieve all results in a specific area ordered by create date. 理想情况下,我会创建坐标的复合键索引并创建日期,然后快速检索由创建日期排序的特定区域中的所有结果。

Is this possible, or must I do my sorting post-query? 这可能,或者我必须在查询后进行排序吗?

However, I was wondering if there was a way to specify sorting by the create_date attribute, rather than distance... 但是,我想知道是否有办法通过create_date属性指定排序,而不是距离......

If you're using the $near command then you have to first sort by distance or the concept of "near" doesn't really make any sense. 如果你正在使用$near命令,那么你必须先按距离排序,否则“近”的概念实际上没有任何意义。 On a globe everything can be "near" to a given point, it's just an issue of "how near". 在地球上,一切都可以“接近”给定点,这只是一个“多近”的问题。

You have two options here: 你有两个选择:

  1. limit the results of $near 限制$near的结果
  2. use the $within command 使用$within命令

I think what you're looking for is the $within command 我认为你要找的是$within命令

center = [50, 50]
radius = 10
db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})

You can then sort these by some other key: 然后,您可以通过其他键对它们进行排序:

db.places.find(...).sort({created:1})

However, the within command may provide too many results, so you probably want to put some logic to limit the number of items returned by $within . 但是,within命令可能会提供太多结果,因此您可能希望使用一些逻辑来限制$within返回的项目数。

db.places.find(...).limit(50).sort({created:1})

Truth is, if you hit a specific limit, the value of your $within command generally begins to drop. 事实是,如果达到特定限制, $within命令的值通常会开始下降。 Your client code may want to check if you're hitting the max results. 您的客户端代码可能想要检查您是否达到最大结果。

As i know for now you can't change default sorting, because it done internally in $near command. 我现在知道你不能改变默认排序,因为它在$ near命令内部完成。

Some notes from documentation : 文档中的一些注释:

db.places.find( { loc : { $near : [50,50] } } ) The above query finds the closest points to (50,50) and returns them sorted by distance (there is no need for an additional sort parameter). db.places.find({loc:{$ near:[50,50]}})上面的查询找到最近的点(50,50)并返回它们按距离排序(不需要额外的排序参数)。 Use limit() to specify a maximum number of points to return (a default limit of 100 applies if unspecified): 使用limit()指定要返回的最大点数(如果未指定,则应用默认限制100):

So you should load collection ordered by distance, and than order by whatever you want on the client. 所以你应该加载按距离排序的集合,而不是按客户端的任何顺序排序。

One more note: If you want sort by date within result sorted by distance you can do it as usual using sort( { date : -1 } ) . 还有一个注意事项:如果您希望按距离排序的结果按日期排序,您可以像往常一样使用sort( { date : -1 } )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM