mongodb mongoose unit of maxDistance

Deadly 提交于 2020-01-10 19:10:31

问题


In my application, I am trying to use, RentModel.find({prop_location : { $near : [msg.lat, msg.lng], $maxDistance : 500}}, function(err, docs){} I have only one value in the database with latitude, longitude of san francisco. Now,

    1. When there is no maxDistance => It finds out the location perfectly from anywhere.
    2. When I add maxDistance as 5 and search exactly at the same location (with same latitude, longitude), it does not find the location.
    3. If it is 50, same as point 2.
    4. If it is 500, I can even find the location from London.

What's wrong ?

what is the unit of distance ? I want to find out everything within 5km (kilometer) distance. will db.executeCommand solve my problem ? need to check.

Thanks in advance.


回答1:


Finally I found the answer to my problem. Theoretically longitude is x-axis and latitude is y-axis. But we are used to latitude- longitude rather than longitude-latitude... So the way I was searching was wrong

$near : [msg.lat, msg.lng]

When I changed this to $near : [msg.lng, msg.lat] , it started working even with 5KM... So ordering was the problem.




回答2:


You need to divide the maxdistance value by 111.12 (one degree is approximately 111.12 kilometers) to convert the degree radius to km distance.

so you can try this

 RentModel.find({prop_location : { $near : [msg.lat, msg.lng], $maxDistance : 500/111.2}})

But the same works different is sphere queries, there you have to divide the degree radius by earth-radius 6371 to work with km values



来源:https://stackoverflow.com/questions/7605813/mongodb-mongoose-unit-of-maxdistance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!