MongoDb C#GeoNear查询构建

时间:2011-11-03 12:46:32

标签: c# mongodb syntax geospatial

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

以下返回距离值不正确的点:

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

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

3 个答案:

答案 0 :(得分:15)

通过thisthis找到答案:

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

答案 1 :(得分:0)

在2.x驱动程序中,GeoNear上不再有IMongoCollection方法。 这是使用MongoDB.Entities便捷库进行$ geoNear查询的一种强类型且简单的方法。

using MongoDB.Driver;
using MongoDB.Entities;

namespace StackOverflow
{
    public class Program
    {
        public class Cafe : Entity
        {
            public string Name { get; set; }
            public Coordinates2D Location { get; set; }
            public double DistanceMeters { get; set; }
        }

        private static void Main(string[] args)
        {
            new DB("test");

            DB.Index<Cafe>()
              .Key(c => c.Location, KeyType.Geo2DSphere)
              .Create();

            (new Cafe
            {
                Name = "Coffee Bean",
                Location = new Coordinates2D(48.8539241, 2.2913515),
            }).Save();

            var searchPoint = new Coordinates2D(48.796964, 2.137456);

            var cafes = DB.GeoNear<Cafe>(
                               NearCoordinates: searchPoint,
                               DistanceField: c => c.DistanceMeters,
                               MaxDistance: 20000)
                          .ToList();
        }
    }
}

上面的代码将以下查询发送到mongodb服务器:

db.Cafe.aggregate([
    {
        "$geoNear": {
            "near": {
                "type": "Point",
                "coordinates": [
                    48.796964,
                    2.137456
                ]
            },
            "distanceField": "DistanceMeters",
            "spherical": true,
            "maxDistance": NumberInt("20000")
        }
    }
])

答案 2 :(得分:0)

这是驱动程序v2.10 +的有效示例。它使用正确的geospatial point字段类型并运行$nearSphere查询。

var longitude = 30d; //x
var latitude= 50d; //y
var point = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(longitude, latitude));
var filter = Builders<TDocument>.Filter.NearSphere(doc => doc.YourLocationField, point, maxGeoDistanceInKm * 1000);
var result = await collection.Find(filter).ToListAsync();

YourLocationField的类型应为GeoJsonPoint<GeoJson2DGeographicCoordinates>。 PS。您也可以像这样为您的字段创建索引以更快地进行搜索:

collection.Indexes.CreateManyAsync(
    new []
    {
        new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Geo2DSphere(it => it.YourLocationField))
    }
);