天天看点

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,如需转载请自行联系原作者