MongoDb C#GeoNear查询构建

Pet*_*ron 10 c# syntax geospatial mongodb

如何使用C#驱动程序和GeoNear方法查询MongoDB附近的地理点?

以下返回具有错误距离值的点:

var results = myCollection.GeoNear(
    Query.GT("ExpiresOn", now), // only recent values
    latitude,
    longitude,
    20
);
Run Code Online (Sandbox Code Playgroud)

我怀疑我应该告诉Mongo查询double [] Location字段,但我不知道查询语法.

Pet*_*ron 15

通过这个这个找到答案:

var earthRadius = 6378.0; // km
var rangeInKm = 3000.0; // km

myCollection.EnsureIndex(IndexKeys.GeoSpatial("Location"));

var near =
    Query.GT("ExpiresOn", now);

var options = GeoNearOptions
    .SetMaxDistance(rangeInKm / earthRadius /* to radians */)
    .SetSpherical(true);

var results = myCollection.GeoNear(
    near,
    request.Longitude, // note the order
    request.Latitude,  // [lng, lat]
    200,
    options
);
Run Code Online (Sandbox Code Playgroud)