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;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class demo003
    {

    static string mongodb = "mongodb://127.0.0.1:27017";
    static string database = "2dshpere";
    static string tblName = "places";
    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"));
        }

        double y = 26.0623344427;
        double x = 119.2916107177;
        double maxDistance = 0.2;//单位公里(千米) 
        //6378137:地球半径,单位:米 
        IMongoQuery query = Query.WithinCircle("loc", x, y, maxDistance / (6378137 / 1000.0), true);
        IMongoQuery query1 = Query.Near("loc", x, y, maxDistance / (6378137 / 1000.0), true);
        IMongoQuery query2 = Query.WithinRectangle("loc", 117.29, 23.00, 117.30, 25.07);

        double[] points = new double[5];
        //var finds = table.Find(query); 
        //var finds = table.Find(query1);
        var finds = table.Find(query2);
        int count = 0;
        //Parallel.ForEach(finds, (item) =>
        //{
        //    Interlocked.Increment(ref count);
        //    Debug.WriteLine(item.ToString());
        //});
        foreach (var item in finds)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine("over");
        Console.ReadLine();
    }
    static void Search(IMongoQuery query)
    {
        List<LbsItem> cards = new List<LbsItem>();
        try
        {
            MongoCursor<BsonDocument> res = table.Find(query).SetLimit(100000).SetSkip(1500000);
            int count = 0;
            var start = DateTime.Now;
            Parallel.ForEach(res, (item) =>
            {
                Interlocked.Increment(ref count);
            });
            double etime = (DateTime.Now - start).TotalSeconds;
            Console.WriteLine(count + ":" + etime + ";" + count / etime);
        }
        catch (Exception ex)
        {
        }
    }
    /// <summary>
    /// 插入测试数据
    /// </summary>
    /// <param name="database"></param>
    static void InsertRecords(MongoDatabase database)
    {
        MongoCollection<BsonDocument> places = database.GetCollection<BsonDocument>("places");
        BsonDocument[] batch = {
                                   new BsonDocument { { "name", "Bran" }, { "loc", new BsonArray(new[] { 119.2916100177, 26.0623344027 }) } },
                                   new BsonDocument { { "name", "Ayla" }, { "loc", new BsonArray(new[] { 117.2916107177, 25.0623344427 }) } },
                                   new BsonDocument { { "name", "Ayla" }, { "loc", new BsonArray(new[] { 116.2916107177, 24.0623344427 }) } },
                                   new BsonDocument { { "name", "Ayla" }, { "loc", new BsonArray(new[] { 117.2916107177, 26.0623344427 }) } },
                                   new BsonDocument { { "name", "Ayla" }, { "loc", new BsonArray(new[] { 118.2916107177, 23.0623344427 }) } }
                                   ,new BsonDocument { { "name", "Ayla" }, { "loc", new BsonArray(new[] { 117.2916107177, 25.0723344427 }) } }
        };
        places.InsertBatch(batch);
    }
}


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

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值