C# MongoDB driver 2.0 - Getting distance back from near query

2.5k views Asked by At

I am doing a NearSphere query with the C# MongoDB driver 2.0 and it works fine. The results are ordered by distance automatically but I would like to get that distance back for each of the search results to be able to display it back. I found this post that says how to do it for the old version of the driver Retrieving the Distance "dis" result from a Near query but didn't manage to find how to do it with the new drivers.

This is my code:

var collection = database.GetCollection<MyType>("myTypes");
var locFilter = Builders<MyType>.Filter.NearSphere(x => x.GeoLocation, criteria.Long, criteria.Lat, criteria.RadiusInMiles/3963.2);
var results = await collection.Find(locFilter).ToListAsync();

I guess I have to do something before calling ToList on the IFindFluent result ?

Any help ?

Thanks a lot

1

There are 1 answers

6
Maximiliano Rios On

The C# MongoDB Driver lacks of some operations but the reality there's not any restriction querying the database. The methods Project, Match and so forth are just helpers to build a PipeLine that's exactly a BsonDocument like any other.

I had to query the database from C# using a similar approach to yours:

db.mycoll.aggregate( 
[ { $geoNear : 
   { near : { type : "Point", coordinates : [-34.5460069,-58.48894900000001] }, 
    distanceField : "dist.calculated",  maxDistance : 100, 
    includeLocs : "dist.location",
    num : 5,  spherical : true   } 
  } , 
  { $project : {_id: 1, place_id:1, name:1, dist:1} } 
] ).pretty()

As you know there's not a geoNear method to build it in the driver, so you can just create the BsonDocument. To show you that everything can be built in that way find below a sample query from C# not using the project clausule either, I built it from the BsonDocument. You can push BsonDocument's in the pipeline as you wish.

var coll = _database.GetCollection<UrbanEntity>("mycoll");
var geoNearOptions = new BsonDocument {
    { "near", new BsonDocument {
        { "type", "Point" }, 
        { "coordinates", new BsonArray {-34.5460069,-58.48894900000001} },
        } },
    { "distanceField", "dist.calculated" },
    { "maxDistance", 100 }, 
    { "includeLocs", "dist.location" },  
    { "num", 5 },  
    { "spherical" , true }
};

var projectOptions = new BsonDocument {
    { "_id" , 1 }, 
    { "place_id", 1 }, 
    { "name" , 1 }, 
    { "dist", 1}
};

var pipeline = new List<BsonDocument>();
pipeline.Add( new BsonDocument { {"$geoNear", geoNearOptions} });
pipeline.Add( new BsonDocument { {"$project", projectOptions} });

using(var cursor = await coll.AggregateAsync<BsonDocument>(pipeline)) {
    while(await cursor.MoveNextAsync()) {
        foreach (var doc in cursor.Current) {
            // Here you have the documents ready to read
        }
    }
}

I hope it helps.