首页 文章

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

提问于
浏览
3

我有以下查询,它使用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
   }
]

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

1 回答

  • 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

相关问题