Mongodb$geoIntersect发行
我有一个藏品,其“地点”如下所示Mongodb$geoIntersect发行,mongodb,geolocation,mongodb-query,Mongodb,Geolocation,Mongodb Query,我有一个藏品,其“地点”如下所示 "place" : { "name" : "Central Region", "country" : "Country_A", "coor_type" : "Polygon", "coordinates" : [ [ [ 103.749959507073, 1.2123138339349 ],
"place" : {
"name" : "Central Region",
"country" : "Country_A",
"coor_type" : "Polygon",
"coordinates" : [
[
[
103.749959507073,
1.2123138339349
],
[
103.918426999964,
1.2123138339349
],
[
103.918426999964,
1.36874499902569
],
[
103.749959507073,
1.36874499902569
]
]
]
}
我正在尝试使用$geointerst操作符返回一个文档列表,其中place.coordinates与由两点定义的矩形相交
[ [103.591247, 1.222136], [104.040313, 1.477497] ]
其中第一个点是矩形的左下角,另一个右上角
我写了一个问题:
db.Document.find(
{'place.coordinates':
{$geoIntersects:
{$geometry:
{type:'Polygon'
, coordinates:[[103.591247,1.222136],[104.040313,1.477497]]
}
}
}
}
);
然而,mongodb一直在抱怨
error: {
"$err" : "Malformed geo query: { $geoIntersects: { $geometry:
{ type:\"Polygon\",
coordinates:[[103.591247,1.222136],[104.040313,1.477497]]
}}}",
"code" : 16677}
我试过不同的类型,比如“Box”和“LineString”。框将给出类似的错误。LineString不会抱怨,但也不会返回任何结果
我做错了什么?有什么建议吗?主要问题是
放置
文档和查询不是正确的文档
下面是如何修复它
1)
线性化的第一个和最后一个坐标必须相同
多边形由一组GeoJSON线性坐标数组组成。这些线条是闭合的线条字符串。闭合线字符串至少有四个坐标对,并指定与第一个和最后一个坐标相同的位置。
--
2)
将coor_type
重命名为type
,以符合GeoJSON标准
{ <location field>: { type: "<GeoJSON type>" , coordinates: <coordinates> } }
3)
确保查询具有正确的多边形值,并且在包含类型
和坐标
字段的字段上进行搜索。
您正在搜索字段place.coordinates
,而它本应是place
示例$geoIntersects查询
存储的数据或查询示例均无效。GeoJSON中的多边形需要以原点开始和结束,其他坐标对代表每个顶点。谢谢,Neil。了解了。
var obj = {
"place": {
"name": "Central Region",
"country": "Country_A",
"type": "Polygon",
"coordinates": [
[
[
103.749959507073,
1.2123138339349
],
[
103.918426999964,
1.2123138339349
],
[
103.918426999964,
1.36874499902569
],
[
103.749959507073,
1.36874499902569
],
[
103.749959507073,
1.2123138339349
]
]
]
}
};
db.geo.drop();
db.geo.insert( obj );
//Use the 2dsphere index to validate your GeoJSON format
// and speed up queries
db.geo.ensureIndex({ "place" : "2dsphere" })
db.geo.find({
place: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [
[
[
103.749959507073,
1.2123138339349
],
[
103.918426999964,
1.2123138339349
],
[
103.918426999964,
1.36874499902569
],
[
103.749959507073,
1.36874499902569
],
[
103.749959507073,
1.2123138339349
]
]
]
}
}
}
})