【问题标题】:C# MongoDB driver 2.0 - Getting distance back from near queryC# MongoDB 驱动程序 2.0 - 从近处查询返回距离
【发布时间】:2015-09-01 10:52:23
【问题描述】:

我正在使用 C# MongoDB 驱动程序 2.0 进行 NearSphere 查询,它工作正常。 结果自动按距离排序,但我希望为每个搜索结果恢复该距离,以便能够将其显示回来。 我发现这篇文章说明了如何为旧版本的驱动程序 Retrieving the Distance "dis" result from a Near query 执行此操作,但没有设法找到如何使用新驱动程序执行此操作。

这是我的代码:

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();

我想在 IFindFluent 结果上调用 ToList 之前我必须做些什么?

有什么帮助吗?

非常感谢

【问题讨论】:

  • 这里实现的基本$near 运算符(在后台)不会返回与给定点或对象的“距离”。您可能需要使用更直接的调用方法:1:聚合$geoNear 或2:geoNear 数据库命令表单,它们都将距离作为返回文档结果中的字段返回
  • 感谢您的帮助!不过,我正在努力寻找使用 C# 2.0 驱动程序的方法。如果你有一个例子将不胜感激。谢谢
  • 现在才找我,因为评论反对“答案”,因为我要去睡觉了。所有驱动程序都支持提交.aggregate()管道或基本db.command语句的基本方法,如果你只是寻找方法
  • 我知道这是一篇旧帖子,但这个问题解决了吗?我在尝试使用聚合时遇到了一个非常相似的问题,但似乎无法让它与近球体一起使用。很想知道这个的状态
  • 我现在就写答案,希望对你有用

标签: c# mongodb geolocation location distance


【解决方案1】:

C# MongoDB 驱动程序缺少一些操作,但实际上查询数据库没有任何限制。 Project、Match 等方法只是构建 PipeLine 的帮手,该 PipeLine 与其他任何 BsonDocument 完全一样。

我不得不使用与您类似的方法从 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()

如您所知,在驱动程序中没有构建它的 geoNear 方法,因此您只需创建 BsonDocument。 为了向您展示一切都可以以这种方式构建,请在下面的 C# 示例查询中找到,也不使用项目子句,我从 BsonDocument 构建它。您可以根据需要将 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
        }
    }
}

希望对你有帮助。

【讨论】:

  • 无论如何我可以完全使用Builders 方法吗?因为我的行为很奇怪,而且只有很少的文件遵循答案
  • 您是否只使用 Builders 语法?
  • 我的 28 个收藏中有近 100 个请求,现在是的...... :(
  • Builders 只是构造 BsonDocuments 的包装器,只是一个助手。您遇到了哪些奇怪的行为?
  • 嗯,现在它似乎可以工作了,文档指出如果只有一个索引,您应该精确搜索要搜索的字段。但是我在文档的字段中得到了 DIST,这使得它无法反序列化......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多