简体   繁体   English

PyMongo-MaxDistance操作正常,但结果不符合预期

[英]PyMongo - MaxDistance operation working but results are not as expected

I have following pymongo query to retrieve geoNear data from MongoDB( 3.0). 我有以下pymongo查询,用于从MongoDB(3.0)中检索geoNear数据。 "distance" is passed in kms and I understand from MongoDB documentation that the "maxDistance" has to be in meters, so I am using "distance * 1000" in my query “距离”以kms为单位传递,我从MongoDB文档中了解到“ maxDistance”必须以米为单位,因此我在查询中使用“ distance * 1000”

 def getNearestVoices(self, longitude, latitude, distance, userid=None, count='500'):
            response = []
            for doc in self.users.aggregate([{"$geoNear": {"near": [longitude, latitude],
                                                           "distanceField":"distance",
                                                           "maxDistance": (distance * 1000),
                                                           "distanceMultiplier": 6371,
                                                           "num": int(count),
                                                           "spherical": True}}]):

                    pass

Now when I pass "1" for distance my expectation is to return "distance" which matches 1 KM or less but this is not happening and all records are returned. 现在,当我通过“ 1”作为距离时,我的期望是返回匹配“ 1公里或更小”的“距离”,但是这没有发生,并且返回了所有记录。

I also noticed one thing, when I change the "distance * 1000" to "distance / 1000" in my mongo query, then I get less records but still not matching my expectation. 我还注意到一件事,当我在mongo查询中将“ distance * 1000”更改为“ distance / 1000”时,得到的记录较少,但仍不符合我的期望。

I am not able to figure out what's wrong in my query. 我无法弄清楚查询中出了什么问题。

PS: I am using distanceMultiplier 6371 to retrieve my output in kms PS:我正在使用distanceMultiplier 6371检索我的输出(以kms为单位)

Thanks 谢谢

Changing 更改

"maxDistance": (distance * 1000)"

to

"maxDistance": (distance / 6371)

worked for me. 为我工作。 Since I am using distance multiplier, I think I have to factor it in 由于我使用的是距离乘数,因此我认为必须考虑在内

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

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