简体   繁体   中英

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).

You should use aggregate near . Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM