经度,纬度和特定距离的MongoDB过滤文件

问题描述:

我的架构设计:经度,纬度和特定距离的MongoDB过滤文件

var LocationSchema = new Schema({ 
    area: String, 
    loc: [ 
    type: [Number], // [<longitude>, <latitude>] 
    index: '2d'  // create the geospatial index 
    ] 

}); 

module.exports = mongoose.model('Location', LocationSchema); 

数据之后还会有存储这样的MongoDB

{ 
    "_id": { 
     "$oid": "58c2796d734d1d46588666c6" 
    }, 
    "area": "madiwla", 
    "loc": [ 
     12.9226, 
     77.6174 
    ] 
} 
{ 
    "_id": { 
     "$oid": "58c27989734d1d4658866705" 
    }, 
    "area": "majestic", 
    "loc": [ 
     12.9767, 
     77.5713 
    ] 
} 

{ 
    "_id": { 
     "$oid": "58ca1e21734d1d2ca8566f3c" 
    }, 
    "area": "shanthi nagar", 
    "loc": [ 
     12.8486, 
     77.6837 
    ] 
} 

我做

db.locations.ensureIndex({loc:"2d"}) 

现在我想要过滤2公里内或5米内的数据 我厌倦了这个查询,但每个evry时间查询返回所有的文件

我已经运行1公里最大距离查询

db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } }) 

我给马拉泰哈利纬度和经度

马拉泰哈利到madiwala距离: - 13.4公里

马拉泰哈利到雄伟的距离: - 20.4千米

marathahalli to shanthi nagar距离: - 23.4千米

但为什么我的查询返回所有文档.IS在我的代码中有任何错误,请帮助我?

我想这也是,但得到错误

db.locations.find({ loc : { 
    $near: { 
    $geometry: { 
      loc: [12.9592,77.6974] 
    }, 
    $maxDistance:0, 
    $minDistance:250 
    } 
} }) 

Error: error: { 
     "waitedMS" : NumberLong(0), 
     "ok" : 0, 
     "errmsg" : "invalid point in geo near query $geometry argument: { loc: [ 
12.9592, 77.6974 ] } Point must be an array or object", 
     "code" : 2 
} 

我mongoshell尝试,但我没有得到任何东西

db.locations.createIndex({loc:"2dsphere"}) 
{ 
     "createdCollectionAutomatically" : false, 
     "numIndexesBefore" : 3, 
     "numIndexesAfter" : 3, 
     "note" : "all indexes already exist", 
     "ok" : 1 
} 
rs-ds161028:PRIMARY> db.locations.aggregate([ {$geoNear: { near :{type 
"Point", coordinates:[77.6974,12.9591]}, distanceField: "distance", spherical: 
true } } ]) 

{ "_id" : ObjectId("58c27989734d1d4658866705"), "area" : "majestic", "loc" : [ 
2.9767, 77.5713 ], "distance" : 8017976.609884486 } 
{ "_id" : ObjectId("58c2796d734d1d46588666c6"), "area" : "madiwla", "loc" : [ 1 
.9226, 77.6174 ], "distance" : 8021105.860532911 } 
{ "_id" : ObjectId("58ca1e21734d1d2ca8566f3c"), "area" : "shanthi nagar", "loc" 
: [ 12.8486, 77.6837 ], "distance" : 8025509.538529409 } 

rs-ds161028:PRIMARY> db.locations.find({ loc:{ $near:{ $geometry: {ty 
e: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}}) 
rs-ds161028:PRIMARY> 
+0

撰写您蒙戈地理查询。 –

+0

你可以帮我吗@VaibhavPatil –

开始我想补充在两台点 -

  1. 使用mongo时,请始终以[long。,lat。]方式插入位置
  2. 请指定要插入的几何体的类型。有关类型的更多信息,请参阅mongoDB documentation

来到您的查询,我加了三个点作为你的问题,我的数据看起来像如下─

> db.test.find() 
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] } 
{ "_id" : "Majestic", "type" : "Point", "loc" : [ 77.5712, 12.9766 ] } 
{ "_id" : "ShanthiNgr", "type" : "Point", "loc" : [ 77.6836, 12.8486 ] } 

我已经给类型:“点”,因为这是使用球型保留关键在蒙戈的位置。此外,我添加了[long,lat]位置。 稍后我在loc字段中添加了2dSphere索引。

db.test.createIndex({loc:"2dsphere"}) 

然后我下面的查询执行要知道从马拉泰哈利[77.6974,12 0.9591]的距离 -

> db.test.aggregate([ {$geoNear: { near :{type: "Point", coordinates:[77.6974,12 
.9591]}, distanceField: "distance", spherical: true } } ]) 
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ], "distance": 9583.305405402776 } 
{ "_id" : "ShanthiNgr", "type" : "Point", "loc" : [ 77.6836, 12.8486 ], "distance" : 12391.53898270671 } 
{ "_id" : "Majestic", "type" : "Point", "loc" : [ 77.5712, 12.9766 ], "distance": 13828.057829128267 } 

以上查询是只是用于测试目的知道的距离。它以米为单位告诉距离,所以我可以很容易地将其转换成公里。

现在我确实运行了下面的查询,从10公里内找到Marathahalli的位置。距离,我得到了我想要的result-

> db.test.find({ loc:{ $near:{ $geometry: {type: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}}) 
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] } 

我发现了一些问题与做法你trying-

  • 位置在[长,LAT]的方式来给出。
  • 类型:使用点类型位置必须给出“点”。它是保留关键字。
  • 距离将在这里以米为单位。在蒙古,很少有地方也采用弧度。有关弧度距离的更多信息,请参阅this mongoDB文档。

希望它可以帮助你......有一个美好的一天... :)

+0

你可以请添加架构设计,以便它对我有所帮助 –

+0

我没有使用任何特定的软件来做到这一点。我只用了mongo shell。正如你在我的回答中看到的,文档看起来像{“_id”:,“type”:“Point”,“loc”:[]}。我同意这不是你采取的文件,但我认为你从某个聚合阶段或使用其他框架粘贴了文档,因为mongo shell无法识别代码中的$ oid。希望你以后可以根据你的需要设计我的文档。让我知道你是否需要进一步的帮助。 –

+0

我也只使用mongoshell,但不工作可以请您添加模式设计 –