$geoWithin with mongoDB aggregate causes BadValue bad geo query

时间:2015-06-15 15:13:16

标签: mongodb aggregation-framework geospatial

I am trying to use a $geoWithin query in a aggregate pipeline, but I am getting a an

MongoError: exception: bad query: BadValue bad geo query: { $geoWithin: {    $box: [ [ "13.618240356445312", "51.01343066212905" ], [ "13.865432739257812",   "51.09662294502995" ] ] } }

My query is:

{ 
    $match: {
        'gps.coordinates.matched': {
            $geoWithin: {
                $box: [
                    [ swlng, swlat ],
                    [ nelng , nelat ]
                ]
            }
        }
    }
},
{ $project : {shortGeohash: {$substr: ["$gps.geohash.original", 0, 11]}}},
{ $group: {_id: "$shortGeohash", count: {$sum:1}, originalDoc:{$push: "$$ROOT"}}}

The query only for $geoWithin as well $project...,$group work well on their own, but combined the error occurs.

2 个答案:

答案 0 :(得分:0)

似乎$ geoWithin不是aggregation operators之一。

遗憾的是,

The reference example有效,我不知道如何添加聚合。

答案 1 :(得分:0)

我尝试了你的查询,它似乎确实有效。我使用这样的文档对集合执行了查询。

[{
    "_id" : "5a2404674eb6d938c8f44856",
    "code" : "M.12345",
    "loc" : {
        "type" : "Point",
        "coordinates" : [ 
            41.9009789, 
            12.5010465
        ]
    }
},
...
]

聚合管道就是这个。

{ 
    $match: {
        'loc': {
            $geoWithin: {
                $box: [
                    [ 0, 0 ],
                    [ 5, 5 ]
                ]
            }
        }
    }
},
{ $project : {subCode: {$substr: ["$code", 0, 4]}}},
{ $group: {_id: "$subCode", count: {$sum:1}, originalDoc:{$push: "$$ROOT"}}}

其中一个结果就是这个。

{
    "_id" : "M.10",
    "count" : 12.0,
    "originalDoc" : [ 
        {
            "_id" : "5a2481c44eb6d92b6895633a",
            "subCode" : "M.10"
        },
        .... //11 more items
    ]
}

使用mongod v3.4.9正确返回结果。