在mongodb聚合操作中,$geoNear - $maxDistance不起作用

4
我有以下查询语句,它根据经纬度使用mongoDB的"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聚合忽略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个回答

4

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接