mongo 查询 距离 某个点 多少 米距离 感谢 提供的数据。 感谢 mvc的 demo 。反正 就是各种感谢 文档之类的。

昨天 去面试来着,

问了一下mong 。

我记得mong支持 地理位置索引的,说了一下。

然后 面试官说 查询某个点 的 多少米范围, 这个该怎么实现? 我懵逼了。。。。

回去 查询了一下。 发现有 测试数据(度量有啊)

//BsonElement bsonele = new BsonElement("address","南京 中华门");

/*
* 创建测试数据:
db.mapinfo.insert({"address" : "南京 禄口国际机场","loc" : { "type": "Point", "coordinates": [118.783799,31.979234]}})
db.mapinfo.insert({"address" : "南京 浦口公园","loc" : { "type": "Point", "coordinates": [118.639523,32.070078]}})
db.mapinfo.insert({"address" : "南京 火车站","loc" : { "type": "Point", "coordinates": [118.803032,32.09248]}})
db.mapinfo.insert({"address" : "南京 新街口","loc" : { "type": "Point", "coordinates": [118.790611,32.047616]}})
db.mapinfo.insert({"address" : "南京 张府园","loc" : { "type": "Point", "coordinates": [118.790427,32.03722]}})
db.mapinfo.insert({"address" : "南京 三山街","loc" : { "type": "Point", "coordinates": [118.788135,32.029064]}})
db.mapinfo.insert({"address" : "南京 中华门","loc" : { "type": "Point", "coordinates": [118.781161,32.013023]}})
db.mapinfo.insert({"address" : "南京 安德门","loc" : { "type": "Point", "coordinates": [118.768964,31.99646]}})
* */

//mong_sql
/*
* db.mapinfo.find(
* {
* "loc" :
* {
* "$near" :
* {
* "$geometry" :
* {
* "type" : "Point", "coordinates" : [118.783799, 31.979234]
* },
* "$maxDistance" : 5000
}
*}
*}
*).limit(50);
*/


//查询的点
double[] array = { 118.803032, 32.09248 };

BsonValue bs = BsonValue.Create(array);

BsonElement bsonele = new BsonElement("coordinates", bs);

var query = new QueryDocument("type", "Point");

query.Add(bsonele);

var query1 = new QueryDocument("$geometry", query);

//距离点的距离多少米
query1.Add("$maxDistance", 50000);

var query2 = new QueryDocument("$near", query1);

var query3 = new QueryDocument("loc", query2);

//IMongoQuery q= Query.EQ("coordinates", bs);

MongoCursor<mapinfo> mi = _mapinfos.Find(query3).SetLimit(50);

感受:

1: 不停地加对象,加到对象和 在mongo里的 对象 一样为止。

2: ado执行的是 SqlServer的sql语句 , 而 mongo.driver 执行的是 bson 。

原文地址:https://www.cnblogs.com/ganmk--jy/p/5635887.html