Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MongoDB驱动程序2.0-从近距离查询返回距离_C#_Mongodb_Geolocation_Location_Distance - Fatal编程技术网

C#MongoDB驱动程序2.0-从近距离查询返回距离

C#MongoDB驱动程序2.0-从近距离查询返回距离,c#,mongodb,geolocation,location,distance,C#,Mongodb,Geolocation,Location,Distance,我正在用C#MongoDB驱动程序2.0进行一个近球查询,它工作得很好。 结果是按距离自动排序的,但我希望每个搜索结果都能显示回距离。 我发现这篇文章说如何为旧版本的驱动程序做这件事,但没有找到如何为新的驱动程序做这件事 这是我的代码: var collection = database.GetCollection<MyType>("myTypes"); var locFilter = Builders<MyType>.Filter.NearSphere(x =>

我正在用C#MongoDB驱动程序2.0进行一个近球查询,它工作得很好。 结果是按距离自动排序的,但我希望每个搜索结果都能显示回距离。 我发现这篇文章说如何为旧版本的驱动程序做这件事,但没有找到如何为新的驱动程序做这件事

这是我的代码:

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();
var collection=database.GetCollection(“myTypes”);
var locFilter=Builders.Filter.NearSphere(x=>x.GeoLocation,criteria.Long,criteria.Lat,criteria.RadiusInMiles/3963.2);
var results=await collection.Find(locFilter.ToListAsync();
我想在打电话给ToList查询IFindFluent结果之前我必须做些什么

有什么帮助吗

非常感谢

C#MongoDB驱动程序缺少一些操作,但实际上查询数据库没有任何限制。Project、Match等方法只是帮助构建一个与其他方法一样完全是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#的示例查询,也不使用project子句,我从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
        }
    }
}
var coll=_database.GetCollection(“mycoll”); var geoNearOptions=新的BsonDocument{ {“近”,新的B文件{ {“类型”,“点”}, {“坐标”,新BsonArray{-34.5460069,-58.4889490000001}, } }, {“distanceField”、“dist.calculated”}, {“最大距离”,100}, {“includeLocs”,“dist.location”}, {“num”,5}, {“球形”,真的} }; var projectOptions=新的BsonDocument{ {u id',1}, {“地点id”,1}, {“名称”,1}, {“dist”,1} }; var pipeline=新列表(); 添加(新的BsonDocument{{“$geoNear”,geoNearOptions}); 添加(新的BsonDocument{{“$project”,projectOptions}); 使用(var cursor=await coll.AggregateAsync(管道)){ while(等待cursor.MoveNextAsync()){ foreach(游标中的var doc.Current){ //这是你准备好阅读的文件 } } }
我希望这会有所帮助。

这里实现的基本运算符(在引擎盖下)不会返回与给定点或对象的“距离”。您可能需要使用更直接的调用方法:1:aggregation或2:database命令表单,这两种方法都会在返回的文档结果中以字段形式返回距离谢谢您的帮助!不过,我正在努力找到使用C#2.0驱动程序实现这一点的方法。如果你有一个例子,我将不胜感激。谢谢你现在就来找我,因为评论反对“回答”,因为我要睡觉了。所有驱动程序都支持提交
.aggregate()
管道或基本
db.command
语句的基本方式,如果您只是查找方法,我知道这是一篇旧文章,但这有解决过吗?我在尝试使用聚合时遇到了一个非常类似的问题,但似乎无法使它与nearsphere一起工作。我很想知道这个问题的状态我现在就要写答案了,我希望你能找到有用的答案我是否可以充分利用
Builders
方法?因为我得到了一个奇怪的行为,只有少数文档遵循回答您是否只使用构建器语法?我在我的28个集合中有将近100个req,现在是…:(构建器只是构建BsonDocuments的包装器,只是一个帮助器。您正在经历哪些奇怪的行为?嗯,现在似乎可以了,文档指出,如果只有一个索引,您应该精确搜索字段。但是,我的文档字段的距离使其无法序列化。。。