【问题标题】:$geoNear - $maxDistance in mongodb aggregation not working$geoNear - mongodb 聚合中的 $maxDistance 不起作用
【发布时间】:2017-07-18 23:22:41
【问题描述】:

我有以下查询,它使用 mongoDB find() 的“2dsphere”索引根据经度和纬度返回结果,但是当我在聚合中使用相同的查询时,$maxDistance 不起作用并且总是显示同样的结果。

db.travelLocationSearch.find({
    location: {
        $near: {
            $geometry: {
                type:  "Point",
                coordinates: [-3.7037901999999576, 40.4167754] // madrid, spain
            },
            $maxDistance: (60*1609.344) // miles to meters
       }
   }
}).count()

60 英里的结果计数 - 147
200 英里 - 170
500 英里 - 671

但是,当我使用 mongo 聚合编写相同的查询时

db.travelLocationSearch.aggregate([
{
    $geoNear: {
        near: { type: "Point", coordinates: [ -3.7037901999999576, 40.4167754 ] },
        distanceField: "dist.calculated",
        maxDistance: (100 * 1609.34), // miles to meter
        distanceMultiplier: 0.000621371, // meter to miles
        includeLocs: "dist.location",
        spherical: true
     }
   }
]).itcount()

60 英里的结果计数 - 100
200 英里 - 100
500 英里 - 100
我还按照此处的说明验证了我的查询 ($geoNear aggregation ignoring maxDistance)
以下是我的索引

> db.travelLocationSearch1.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "zen.travelLocationSearch"
    },
    {
        "v" : 2,
        "key" : {
            "location" : "2dsphere"
        },
        "name" : "location_2dsphere",
        "ns" : "zenbrisa.travelLocationSearch1",
        "2dsphereIndexVersion" : 3
   }
]

请让我知道解决方案。我的基本要求是使用给定的里程和纬度、经度获得结果,并且随着用户增加里程结果会越来越长。

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework geospatial


    【解决方案1】:

    我发现为什么我总是得到 100 个结果,因为默认情况下 $geoNear 返回 100 个结果,这就是为什么我总是看到 100 个结果,在增加限制后,数字从 100 增加到 134 到更多。 最终查询

    db.travelLocationSearch.aggregate([
    {
        $geoNear: {
            near: { type: "Point", coordinates: [ -3.7037901999999576, 40.4167754 ] },
            distanceField: "dist.calculated",
            maxDistance: (100 * 1609.34), // miles to meter
            distanceMultiplier: 0.000621371, // meter to miles
            includeLocs: "dist.location",
            spherical: true,
            num: 1000
        }
    }]).itcount()
    

    结果数:409

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-29
      • 2023-03-21
      • 2018-08-25
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多