$GeoInwithMongoDB聚合导致BadValue错误地理查询
我试图在聚合管道中使用$geointer查询,但得到了一个an$GeoInwithMongoDB聚合导致BadValue错误地理查询,mongodb,aggregation-framework,geospatial,Mongodb,Aggregation Framework,Geospatial,我试图在聚合管道中使用$geointer查询,但得到了一个an MongoError: exception: bad query: BadValue bad geo query: { $geoWithin: { $box: [ [ "13.618240356445312", "51.01343066212905" ], [ "13.865432739257812", "51.09662294502995" ] ] } } 我的问题是: { $match: {
MongoError: exception: bad query: BadValue bad geo query: { $geoWithin: { $box: [ [ "13.618240356445312", "51.01343066212905" ], [ "13.865432739257812", "51.09662294502995" ] ] } }
我的问题是:
{
$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"}}}
仅对
$geointen
以及$project…,$group
的查询本身运行良好,但合并后会出现错误。似乎$geointen不是问题之一
很遗憾,我不知道如何向其中添加聚合。我尝试了您的查询,但它似乎确实有效。我使用这样的文档对集合执行了查询
[{
"_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正确返回结果。Hmm。您是否真的使用
[]
作为正确的数组标识符来包装管道?更不用说,$$ROOT
并没有像我想的那样做。它只是“当前”阶段的“整个”文档,而不是使用$project
修改之前的“原始”文档,我以前没有这样做,但是将其包装为数组会导致相同的错误,没有任何更改。确实,$$ROOT
没有像我预期的那样工作,但它至少给了我ObjectId
,这对我来说很好。你最好编辑你的问题,并提供人们可以测试和复制的示例。现在我只是说,“为我工作”。但是请记住这里的$$ROOT
注释,因为您做了错误的事情。