我有一个包含LineString的文档。是否可以根据一对坐标找到并返回LineString中最近的点?
db.paths.find(
{
loc : {
$near : {
$geometry : {
type : "Point" ,
coordinates : [-73.965355,40.782865]
},
$maxDistance : 20000
}
}
);
// My "path" document
loc:{
"type": "LineString",
"coordinates": [
[-2.551082,48.5955632],
[-2.551229,48.594312],
[-2.551550,48.593312],
[-2.552400,48.592312],
[-2.553677, 48.590898]
]
}
答案 0 :(得分:1)
从MongoDB v2.4(目前为v3.2)开始,LineString
是受支持的GeoJSON对象之一。见GeoSpatial: GeoJSON Objects
在loc
字段中创建2dsphere index后,您可以查询最近的点。
使用您的示例文档,mongo shell查询将是:
db.paths.find({
location:{
$nearSphere:{
$geometry:{
type:"Point",
coordinates:[-2.551010, 48.59123]
},
$maxDistance:2000
}
}
});
请注意,$maxDistance
以米为单位。您的示例find()
坐标远远超出您指定的最大距离。
有关v3.2信息,请参阅$nearSphere或$near运营商。