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

posted @   94cool  阅读(463)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2016-08-02 win2008server R2 x64 部署.net core到IIS上出现【Failed to load the dll from [C:\Program Files\dotnet\host\fxr\1.0.1\hostfxr.dll], HRESULT: 0x80070057】错误
2013-08-02 Android 打包签名 从生成keystore到完成签名 -- 转
2013-08-02 android 对话框中的进度条 (ProgressDialog)
2013-08-02 Android Http Get Post
2013-08-02 Android Integer.parseInt java.lang.NumberFormatException: Invalid int解决方法
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示