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