mel*_*lis 4 pipeline mongodb node.js aggregation-framework
我的文档结构如下:
{ agency_key : '',
route_id: '',
direction_id: '',
stops: [
{stop_id:15,
stop_lat: '',
stop_lon: '',
loc: [-83.118972, 42, 121567]
},
{...
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想在集合中的每个文档的给定距离内找到靠近给定 (lat,lon) 对的停靠点。我创建了二维索引。我尝试了 $unwind,然后是 $geoNear,但它说 $geoNear 只能在管道的第一阶段。我试过这个:
db.tm_stops.aggregate([
... {
... $geoNear: { near: {coordinates: [-82.958841, 42.370114] }, distanceField: "stops.calculated", query: { agency_key: "DDOT"},
... includeLocs: "stops.loc" }
... }
... ])
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
db.tm_stops.find({stops:{$near:[-82.958841, 42.370114], $maxDistance: 1 } } )
Run Code Online (Sandbox Code Playgroud)
它抛出这个错误:
error: {
"$err" : "Unable to execute query: error processing query: ns=gtfs.tm_stops limit=0 skip=0\nTree: GEONEAR field=stops maxdist=1 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
"code" : 17007
Run Code Online (Sandbox Code Playgroud)
我的 mongo 查询应该是什么?我需要从 Node.js 应用程序执行它,但我想先在 Mongo shell 中看到一些结果。提前致谢。
我终于使查询工作。我正在回答我自己的问题以供将来参考。由于我使用的是 2d 索引而不是 2dsphere,因此我不必coordinates在 Near 运算符中使用。此查询有效:
db.tm_stops.aggregate([
{
$geoNear:
{ near: [-82.958841, 42.370114] ,
distanceField: "stops.calculated",
query: { agency_key: "DDOT"},
includeLocs: "stops.loc" }
}
])
Run Code Online (Sandbox Code Playgroud)
但我发现,在返回的文档中,我只能看到计算出的距离和用于计算距离的 (lon, lat) 对。我无法真正返回用于计算的整个嵌入式文档。
如果我找到返回整个嵌入文档的方法,我会更新我的答案。
| 归档时间: |
|
| 查看次数: |
754 次 |
| 最近记录: |