【问题标题】:maxDistance in mongodb : issue in finding near by datamongodb中的maxDistance:在数据附近查找问题
【发布时间】:2016-09-20 01:12:21
【问题描述】:

我的mongo版本是3.2.6

我的桌子是 restaurant_locations。

我给出的索引如下:

db.restaurant_locations._ensureIndex({'location': '2dsphere'});

此表中存储的数据如下:

> db.restaurant_locations.find().pretty();
{
        "_id" : ObjectId("573ff477df3c12bdd6463d07"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        72.5220332,
                        23.0656791
                ]
        }
}
{
        "_id" : ObjectId("573ff5badf3c12bdd6463d08"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        62.5220332,
                        13.0656791
                ]
        }
}

现在当我触发以下查询时:

db.restaurant_locations.find({
  location: {
    $near: {
        $geometry: {
          type: 'Point',
          coordinates: [ 70, 20 ]
        },
    $maxDistance: 100000000
    }
  }
});

我得到两条记录作为输出:

{ "_id" : ObjectId("573ff477df3c12bdd6463d07"), "location" : { "type" : "Point", "coordinates" : [ 72.5220332, 23.0656791 ] } }
{ "_id" : ObjectId("573ff5badf3c12bdd6463d08"), "location" : { "type" : "Point", "coordinates" : [ 62.5220332, 13.0656791 ] } }

现在,当我执行以下查询时:

db.restaurant_locations.find({
  location: {
    $near: {
        $geometry: {
          type: 'Point',
          coordinates: [ 70, 20 ]
        },
    $maxDistance: 1000000
    }
  }
});

我只得到一条记录作为输出:

{ "_id" : ObjectId("573ff477df3c12bdd6463d07"), "location" : { "type" : "Point", "coordinates" : [ 72.5220332, 23.0656791 ] } }
  1. 我不知道 $maxDistance 是如何工作的。
  2. 我想找10公里范围内的餐厅,具体是什么,需要传入$maxDistance?
  3. 假设我的坐标是 40 和 20,那么如果我想要 10 公里内的餐馆,会显示以上两条记录吗?

我参考了许多 mongodb 文档、stackoverflow 问题,但并没有得到确切的想法,它是如何工作的

提前感谢您的帮助

【问题讨论】:

    标签: mongodb geolocation location mongodb-query


    【解决方案1】:

    I am not getting exactly how $maxDistance work.

    为了获得 maxDistance 工作,您必须使用 $nearSphere 函数

    您的查询将是这样的:

    location: {
        $nearSphere: {
            $geometry: {
              type: 'Point',
              coordinates: [ 70, 20 ]
            },
        $maxDistance: 10000
        }
      } 
    

    I want to find restaurants within a distance of 10 kms,

    你必须将它乘以 1000,因为 mongo 分辨率以米为单位

    Suppose my co-ordinates are 40 and 20, so if I want restaurants within 10kms, will above two records display?

    是的,但您必须确保先指定经度,然后指定纬度。

    【讨论】:

    • 我执行了你指定的查询,但它仍然没有给出任何输出,如果我将“10000”替换为“1000000”,我得到一条记录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    相关资源
    最近更新 更多