简体   繁体   English

C#MongoDB驱动程序2.0 - 从近处查询获得距离

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

I am doing a NearSphere query with the C# MongoDB driver 2.0 and it works fine. 我正在使用C#MongoDB驱动程序2.0进行NearSphere查询,它工作正常。 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. 我发现这篇文章说明如何为旧版本的驱动程序执行此操作从Near查询中检索距离“dis”结果但是没有设法找到如何使用新驱动程序执行此操作。

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 ? 我想在调用IFindFluent结果上的ToList之前我必须做些什么?

Any help ? 有帮助吗?

Thanks a lot 非常感谢

The C# MongoDB Driver lacks of some operations but the reality there's not any restriction querying the database. C#MongoDB驱动程序缺少一些操作,但实际上查询数据库没有任何限制。 The methods Project, Match and so forth are just helpers to build a PipeLine that's exactly a BsonDocument like any other. Project,Match等方法只是帮助构建一个与任何其他方法完全一样的BsonDocument的PipeLine。

I had to query the database from C# using a similar approach to yours: 我不得不使用与您类似的方法从C#查询数据库:

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. 如您所知,在驱动程序中没有geoNear方法来构建它,因此您只需创建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. 为了向您展示可以以这种方式构建所有内容,请在C#中查找不使用项目clausule的示例查询,我是从BsonDocument构建的。 You can push BsonDocument's in the pipeline as you wish. 您可以根据需要在管道中推送BsonDocument。

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. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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