c# 实现的mongodb空间索引,空间查询(一)

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GeoJsonObjectModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    // > db.LbsItem.findOne();
    //{
    //        "_id" : ObjectId("5d315dcc4b0142ec4e44f6e5"),
    //        "name" : "Alice-0",
    //        "loc" : {
    //                "type" : "Point",
    //                "coordinates" : [
    //                        158.78458362714602,
    //                        58.53367549904328
    //                 ]
    //                }
    //}
    //>
    class demo001
    {

    static string mongodb = "mongodb://127.0.0.1:27017";
    static string database = "2dshpere";
    static string tblName = "LbsItem";
    static MongoCollection<BsonDocument> table;
    public static void Func()
    {
        MongoClient client;
        MongoServer server;
        MongoDatabase db;
        MongoClientSettings setting = new MongoClientSettings();
        setting.MaxConnectionPoolSize = 1000;
        setting.MinConnectionPoolSize = 500;
        client = new MongoClient(mongodb);


        server = client.GetServer();
        db = server.GetDatabase(database);
        table = db.GetCollection(tblName);
        //InsertRecords(db);
        #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
        if (!table.IndexExistsByName("loc_2dsphere"))
        {
            table.CreateIndex(IndexKeys.GeoSpatialSpherical("loc"));
        }
        if (!table.IndexExistsByName("loc.coordinates_2dsphere"))
        {
            table.CreateIndex(IndexKeys.GeoSpatialSpherical("loc.coordinates"));
        }
        double y = 34.7550188169279;
        double x = 93.0100000;
        double maxDistance = 200;//单位公里(千米) 
                                 //6378137:地球半径,单位:米 
                                 //圆形
                                 //IMongoQuery query = Query.WithinCircle("loc", x, y, maxDistance / (6378137 / 1000.0), true);
                                 //最近  弧度true,度false
                                 //IMongoQuery query = Query.Near("loc", x, y, maxDistance / (6378137 / 1000.0), true);
                                 //矩形
        IMongoQuery query = Query.WithinRectangle("loc.coordinates", 118.0, 24.0, 120.0, 30.0);//
        
        //var queryGeo = "{'loc':{$geoWithin:{$box: [[118.0, 24.0],[ 120.0,30.0 ]]}}}";
        //var filter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(queryGeo);
        
        //var result = table.Find(filter.to).ToList();
        //多边形

        Stopwatch sw = new Stopwatch();
        sw.Start();
        var finds = table.Find(query).ToList();

        long count = finds.Count();
        sw.Stop();
        long ts = sw.ElapsedMilliseconds;
        //foreach (var item in finds)
        //{
        //    Console.WriteLine(item);
        //}
        Console.WriteLine("数量:" + count);
        Console.WriteLine("over耗时:" + ts);
        Console.ReadLine();
    }

    /// <summary>
    /// 插入测试数据
    /// </summary>
    /// <param name="database"></param>
    static void InsertRecords(MongoDatabase database)
    {
        MongoCollection<BsonDocument> places = database.GetCollection<BsonDocument>("LbsItem");
        Random rd = new Random();
        List<LbsItem> list = new List<LbsItem>();
        for (int i = 0; i < 500000; i++)
        {
            LbsItem li = new LbsItem();
            li.name = "Alice-" + i;
            li.loc = GeoJson.Point(new GeoJson2DGeographicCoordinates(rd.NextDouble() * 179.0, rd.NextDouble() * 89.0));
            list.Add(li);
        }

        places.InsertBatch(list);
    }
}


///(重要的是要注意数组中的顺序,即经度,纬度 – 遵循x,y的更逻辑顺序,而不是纬度先于经度的更常用形式):
public class LbsItem
{
    ///
    /// <summary> 
    /// 终端标识 
    /// </summary> 
    public string name
    {
        get; set;
    }
    /// <summary> 
    /// 
    /// </summary> 
    public GeoJsonPoint<GeoJson2DGeographicCoordinates> loc { get; set; }

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值