Hiren Gevariya
Hiren Gevariya

Reputation: 1

Find to Near by Geo location using Latitude and Longitude

geoNear: function (lat, lon) { return new Promise((resolve) => { UserLocation.geoNear({type: 'Point', coordinates: [parseFloat(query.lat), parseFloat(query.lon)]}, {distanceField: 'dist.calculated', maxDistance: 100, spherical: true}, function (error, data) { console.log('Error', error); resolve((error) ? {} : (data == null) ? {} : data) }) }) },

Upvotes: 0

Views: 290

Answers (1)

Ashwanth Madhav
Ashwanth Madhav

Reputation: 1134

Let the document like

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

You can query using

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

For more details $geoNear

Upvotes: 0

Related Questions