mongodb mongoose unit of maxDistance mongodb mongoose unit of maxDistance mongoose mongoose

mongodb mongoose unit of maxDistance


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.


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