[英]MongoDB - output distance without sorting
I am new to MongoDB and I am jumping right to the deep water of geo-spatial queries. 我是MongoDB的新手,我正跳到地理空间查询的深水。 I noticed that the geoNear
command and the $geoNear
aggregation operator can output the calculated distance (using distanceField
). 我注意到geoNear
命令和$geoNear
聚合运算符可以输出计算出的距离(使用distanceField
)。
The command and the operator also sort the results by distance. 该命令和操作员还按距离对结果进行排序。
Is there a way to output the distance without sorting by distance? 有没有一种方法可以输出距离而不按距离排序? ie use some projection operator. 即使用一些投影运算符。 This could obviously be done using a map
with a javascript function to calculate the distance manually, but it seems there is a built-in distance calculation function that must be much more efficient. 显然,这可以通过使用带有javascript函数的map
来手动计算距离来完成,但似乎有一个内置的距离计算功能必须更高效。 Also, it seems to be inefficient to use the $geoNear
operator and afterwards (in the aggregation pipeline) resort the results... right? 另外,使用$geoNear
运算符似乎效率低下,之后(在聚合管道中)求助于结果...对吗?
Thanks 谢谢
Due to the num and limit parameters to a $geoNear
query, (which are mutually exclusive -- with num overriding limit if both are present, see docs ), the results have to be sorted by distance to determine which ones to return. 由于$geoNear
查询的num和limit参数(它们是互斥的-如果同时存在num覆盖限制,请参阅docs ),结果必须按距离排序以确定要返回的结果。 So, in fact, no extra work is being done in returning the results sorted. 因此,实际上,在返回排序后的结果时无需进行任何额外的工作。 As far as I know, it is not possible to return results unsorted by distance. 据我所知,不可能返回未按距离排序的结果。
This makes particular sense in the case of sharding, which cannot currently be done on a geo key , as the query has to be sent to all shards, and the results then combined and returned according to the values of num and/or limit. 这在分片的情况下特别有意义,因为分片目前无法在地理键上完成,因为必须将查询发送到所有分片,然后根据num和/或limit的值组合并返回结果。
MongoDB does not use an R-tree index, as some spatial databases do, which can make distance searches somewhat more efficient, but a B-tree on a geohash, so a distance query would work by comparing the distances in the nearest quadrants, until a sufficient number are found, which is likely to be more than num or limit and then returning only that number, after sorting on distance. MongoDB不像某些空间数据库那样使用R树索引,这可以使距离搜索更有效,但是使用geohash上的B树,因此距离查询可以通过比较最近象限中的距离来进行,直到找到足够的数字,该数字可能大于num或limit,然后在按距离排序后仅返回该数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.