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
{
    class demo002
    {

    static string mongodb = "mongodb://127.0.0.1:27017";
    static string database = "2dshpere";
    static string tblName = "Places2";
    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 = 34.7550188169279;
        double x = 93.0100000;
        double maxDistance = 200;//单位公里(千米) 
        //6378137:地球半径,单位:米 
        //IMongoQuery query = Query.WithinCircle("loc", x, y, maxDistance / (6378137 / 1000.0), true);
        //IMongoQuery query = Query.Near("loc", x, y, maxDistance / (6378137 / 1000.0), true);
        IMongoQuery query = Query.WithinRectangle("loc", 93.0174104175611, 34.7650188169279, 121.0674104175611, 50.8150188169279);
        Stopwatch sw = new Stopwatch();
        sw.Start();
        //var finds = table.Find(query); 
        var finds = table.Find(query);
        int count = 0;

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

    /// <summary>
    /// 插入测试数据
    /// </summary>
    /// <param name="database"></param>
    static void InsertRecords(MongoDatabase database)
    {
        MongoCollection<BsonDocument> places = database.GetCollection<BsonDocument>("Places2");


        Places[] batch = new Places[50000];
        Random rd = new Random();

        for (int i = 0; i < 50000; i++)
        {
            Places ps = new Places();
            Coordinate cd = new Coordinate();
            cd.longitude = rd.NextDouble() * 179.0;
            cd.latitude = rd.NextDouble() * 89.0;
            ps.name = "Alice-" + i;
            ps.loc = cd;
            batch[i] = ps;
        }
        places.InsertBatch(batch);
    }
}


///(重要的是要注意数组中的顺序,即经度,纬度 – 遵循x,y的更逻辑顺序,而不是纬度先于经度的更常用形式):
class Coordinate
{
    public double longitude { get; set; }
    public double latitude { get; set; }
}

class Places
{
    public string name;
    public Coordinate loc;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值