Unit mongob mongoose maxDistance

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

    • When not maxDistance => It finds out the location perfectly from anywhere.
    • When I add maxDistance as 5 and search exactly the same location (with the same latitude, longitude), it does not find the location.
    • If it is 50, then the same as point 2.
    • If it's 500, I can even find a place from London.

What happened?

what unit of distance? I want to know everything within 5 km (km). will db.executeCommand solve my problem? Need to check.

Thanks in advance.

+7
source share
2 answers

Finally, I found the answer to my problem. Theoretically, longitude is the x axis, and latitude is the y axis. But we are used to latitude - longitude, not longitude - latitude ... So I was looking for the wrong

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

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

+10
source

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

so you can try it

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

But the same different jobs are spherical queries, there you need to divide the radius of the degree along the radius of the Earth 6371 to work with the values โ€‹โ€‹of km

+7
source

All Articles