MongoDB Geospacial搜索和官方C#驱动程序

use*_*558 6 c# mongodb mongodb-.net-driver

有些专家可以指出在MongoDB中使用官方C#驱动程序进行Geospacial搜索的最佳方法.最佳对象构造函数(字符串/双精度),构建索引,查找附近.非常感谢您的帮助.

db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } ),  
db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } ).limit(20),
Run Code Online (Sandbox Code Playgroud)

Rob*_*tam 8

与Mongo shell命令等效的C#是:

places.EnsureIndex(IndexKeys.GeoSpatial("loc"), IndexOptions.SetGeoSpatialRange(-500, 500));
var query = Query.Near("loc", 50, 50, 5);
var cursor = places.Find(query).SetLimit(20);
foreach (var hit in cursor) {
    // process hit
}
Run Code Online (Sandbox Code Playgroud)