Mongodb查询纬度和经度点之间的距离

Mongodb Query for finding distance between latitude and longitude points

我有一个 user 的集合,每个用户的 Latitudelongitude 点.

I want to calculate distance between given Lat, long values and user's location.

在MYSQL中有一个查询

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

mongodb中是否有任何等效的方法/查询?在 Javascript 中有一些方法可以实现这一点,但如果在查询中编码会更好。

Mongo 不会像在 sql 世界中那样为您提供简单的字段操作。一些 geospatial results give you back the distance as well, but for the task you want, I would rather do this calculation afterwards. There is a question on SO which targets you perfectly and among many answers I gave an optimized javascript 解决方案。