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

來源:互聯網
上載者:User

標籤:資料   oca   tab   index   cat   let   座標   對象   ota   


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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.