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

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

本文介绍了MongoDB GeoNear的结果除了距离以外的其他东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个PHP应用程序,我们需要在某个边界内检索结果,但是按结果的创建日期排序,而不是距离。我认为MongoDB的geoNear命令对此很有用,因为它负责计算每个结果的距离。但是,我想知道是否有一种方法可以通过create_date属性指定排序,而不是距离。理想情况下,我会创建坐标的复合键索引并创建日期,然后快速检索按创建日期排序的特定区域中的所有结果。

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. I figured MongoDB's geoNear command would be great for this, since it takes care of calculating the distance for each result. However, I was wondering if there was a way to specify sorting by the create_date attribute, rather than distance. 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?

推荐答案

如果您使用 $在命令附近,你必须先按距离排序,否则近的概念实际上没有任何意义。在地球上,一切都可以接近给定点,这只是一个多近的问题。

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. On a globe everything can be "near" to a given point, it's just an issue of "how near".

这里有两个选择:


  1. 限制 $附近的结果

  2. 使用 $ 命令

  1. limit the results of $near
  2. use the $within command

我认为你要找的是 $ 命令

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})

但是,within命令可能提供太多结果,所以你可能想要一些逻辑来限制 $在中返回的项目数。

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.

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. Your client code may want to check if you're hitting the max results.

这篇关于MongoDB GeoNear的结果除了距离以外的其他东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:16