【问题标题】:mongodb filter documents with longitude,latitude and with particular distancemongodb过滤具有经度,纬度和特定距离的文档
【发布时间】:2017-08-07 04:04:00
【问题描述】:

我的架构设计:

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"})

现在我想过滤 2km 或 5m 内的数据 我厌倦了这个查询,但每次查询都会返回所有文档

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

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

我给了马拉松的经纬度

marathahalli 到 madiwala 的距离:- 13.4 公里

马拉松到雄伟的距离:- 20.4 公里

marathahalli 到 shanthi nagar 距离:- 23.4 公里

但是为什么我的查询返回了所有的文档。我的代码中是否有任何错误请帮助我?

我也试过了,但是出错了

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>

【问题讨论】:

  • 写下你的 mongo 地理查询。
  • 你能帮我吗@VaibhavPatil

标签: node.js mongodb express mongoose mongoose-schema


【解决方案1】:

在开始之前我会补充两点-

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

我给出了 type: "Point" 因为这是在 mongo 中使用球形类型位置的保留键。此外,我为位置添加了 [long, lat]。 后来我在 loc 字段上添加了一个 2dSphere 索引。

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

然后我执行以下查询以了解与 Marathahalli [77.6974,12] 的距离 .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 }

以上查询仅用于测试目的以了解距离。它以米为单位表示距离,因此我可以轻松地将其转换为公里。

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

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

我发现您尝试的方法很少有问题-

  • 位置必须以 [long, lat] 方式给出。
  • type:"Point" 必须给出使用点类型位置。它是保留关键字。
  • 此处的距离将以米为单位。在少数地方,它在 Mongo 中也采用弧度。有关弧度距离的更多信息,请参阅thismongoDB 文档。

希望对您有所帮助...祝您有美好的一天...:)

【讨论】:

  • 能否请您添加架构设计,以便对我有所帮助
  • 我没有使用任何特定的软件来执行此操作。我只是使用了 mongo shell。正如您在我的回答中看到的那样,文档看起来像 {"_id" : , "type" : "Point", "loc" : [ ]}。我同意这不是您获取的文档,但我认为您从某个聚合阶段或使用其他框架粘贴了该文档,因为 mongo shell 无法识别代码中的 $oid。希望您以后可以根据您的需要设计我的文档。如果您需要进一步的帮助,请告诉我。
  • 我也只使用 mongoshell,但它不起作用,请您添加架构设计
  • 我没有使用任何架构的东西。我刚刚进入测试集合并插入了这三个文档。我对您所说的架构知之甚少。我认为 mongo 是少模式,在这里遵循模式不是强制性的....
  • 把你使用的确切数据发给我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 2011-08-16
  • 2011-10-01
  • 1970-01-01
相关资源
最近更新 更多