MongoDB 2.4 以降では、GeoJSON を格納してインデックスを作成します。ここですべての概念を見つけることができます。
POCO タイプで GeoJSON プロパティを定義する方法:
public class Foo
{
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
}
インスタンス化の例:
var docToInsert = new Foo
{
Location = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(
new GeoJson2DGeographicCoordinates(-121.97620341421, 37.503287248864))
});
$nearには地理空間インデックスが必要です。また、GeoJSON を格納しているため、具体的には 2dsphere インデックスが必要です。
var collection = //get MongoCollection<Foo> from MongoDatabase object
collection.EnsureIndex(IndexKeys<Foo>.GeoSpatialSpherical(x => x.Location));
今クエリ:
var point = GeoJson.Point(GeoJson.Geographic(-96.770401, 32.816774)); //long, lat
var locationClause = Query<Foo>.Near(y=> y.Location, point, 20); //20 is max distance from your question. note that it's in meters
IQueryable<Foo> query = collection.AsQueryable().Where( x=> locationClause.Inject());
//or with the non-generic Query:
IQueryable<Foo> query = collection.AsQueryable().Where( x=> Query.Near("Location", point, 20).Inject());