$ geoNear - mongodb聚合中的$ maxDistance不起作用

时间:2017-02-28 05:49:19

标签: mongodb mongodb-query aggregation-framework geospatial

我有以下查询,使用" 2dsphere"返回基于经度和纬度的结果。 mongoDB find()的索引,但是当我在聚合中使用相同的查询时,$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 aggregate

编写相同的查询时
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
   }
]

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

1 个答案:

答案 0 :(得分:2)

我发现为什么我总是得到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

相关问题