MongoDb C# GeoNear Query Construction

后端 未结 3 847
悲哀的现实
悲哀的现实 2020-12-16 19:15

How do I query MongoDB for nearby geographic points using the C# driver and the GeoNear method?

The following returns points with an incorrect Distance

3条回答
  •  别那么骄傲
    2020-12-16 19:25

    Found the answer via this and this:

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

提交回复
热议问题