首页 > 解决方案 > 使用纬度和经度按地理位置查找到附近

问题描述

geoNear: function (lat, lon) { return new Promise((resolve) => { UserLocation.geoNear({type: 'Point', 坐标: [parseFloat(query.lat), parseFloat(query.lon)]}, { distanceField:'dist.calculated',maxDistance:100,球形:true},函数(错误,数据){console.log('错误',错误);解析((错误)?{}:(数据==空) ? {} : 数据) }) }) },

标签: node.jsmongodbmongoose

解决方案


让文件喜欢

{
"_id" : 3,
"name" : "Polo Grounds",
"location": {
  "type" : "Point",
  "coordinates" : [ -73.9375, 40.8303 ]
}
}

您可以使用查询

db.collection.aggregate([
{
 $geoNear: {
    near: { type: "Point", coordinates: [ -73.98142 , 40.71782 ] },
    key: "location",
    distanceField: "dist.calculated"
 }
}
])

更多详情$geoNear


推荐阅读