Barranger Ridler
Barranger Ridler

Reputation: 880

Retrieving the Distance "dis" result from a Near query

I've set up a geo-spacial query that works beautifully, however I'm looking to get the distance for each of the results.

Given the query:

var query = Query.Near("Location", longitude, latitude);
var places = mongoDb.GetCollection<Place>("places").Find(near);

how do I retrieve the distance from each element in "places"?

Upvotes: 2

Views: 1326

Answers (1)

Michael DeBerry
Michael DeBerry

Reputation: 183

I don't know if you can get the distance from mongo, but what I have done in the past was just using the Haversine formula to get the distance after I had the data.

You can find the implementation that I used here

EDIT: Sorry you can get the distance back from Mongo, but not with Query.Near(). You need to use the GeoNear function that is on the collection.

 mongoDb.GetCollection<Place>("places").GeoNear(Query.Null, latitude, longitude, maxDistance);

This will return a GeoNearResult<Place> which will have a Hits property that is IEnumurable<Hit> that will contain a Distance property and a document property that will contain your place object.

Upvotes: 3

Related Questions