天天看點

c# 在mongo中查詢經緯度範圍

#region 索引

//IndexKeysDocument doc = new IndexKeysDocument();//建立索引

//2d 平面坐标索引,适用于基于平面的坐标計算。也支援球面距離計算,不過官方推薦使用2dsphere索引 

//BsonValue value = BsonValue.Create("2d");//建立2d索引

//2dsphere 幾何球體索引,适用于球面幾何運算 

//不過,隻要坐标跨度不太大(比如幾百幾千公裡),這兩個索引計算出的距離相差幾乎可以忽略不計 

//BsonValue value = BsonValue.Create("2dsphere");//建立2d索引

//doc.Add("loc", value);//loc為資料庫中2d索引的對象名稱 

//table.CreateIndex(doc);//建立索引

#endregion

double y = 26.0623344427; 

 double x = 119.2916107177; 

 double maxDistance = 0.5;//機關:公裡(千米) 

double earchR = 6378137 / 1000.0;//6378137:地球半徑,機關:米 

double distance = maxDistance / earchR;

IMongoQuery query = Query.WithinCircle("loc", x, y, distance, true); 

 IMongoQuery query1 = Query.Near("loc", x, y, distance, true);

var _location = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(x, y)); 

 IMongoQuery query2 = Query.Near("loc", _location, distance, true);

ConcurrentBag<double> bags = new ConcurrentBag<double>(); 

 double totaletime = 0; 

 int max = 1000; 

 Parallel.For(0, max, (i) => 

 { 

    DateTime start = DateTime.Now; 

    var finds = table.Find(query1).AsQueryable().ToList(); 

    double etime = (DateTime.Now - start).TotalSeconds; 

    bags.Add(etime); 

 });

本文轉自94cool部落格園部落格,原文連結:http://www.cnblogs.com/94cool/p/7272933.html,如需轉載請自行聯系原作者