Faizuddin Mohammed
Faizuddin Mohammed

Reputation: 4328

How do I calculate distance between two geospatial in mongoose?

I have to locate all the places near a point (GeoJson). I can easily use near method on the Model to find them.

But, I also need the distances from the current location to be stored in the final output as a virtual property.

I believe I should use geoNear command somewhere but I am a complete noob to MongoDB or mongoose(I'm using v4) and do not know what commands are or how to use them (particularly in mongoose).

Upvotes: 2

Views: 2701

Answers (1)

Diego Haz
Diego Haz

Reputation: 938

You should use aggregate near. Example:

mongoose.model('ModelName').aggregate().near({
  near: [lng, lat],
  distanceField: 'distance'
})

Upvotes: 3

Related Questions