Mongodb $geoIntersect 问题
Mongodb $geoIntersect issue
我有一个 属性 'place' 的集合,如下所示。
"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
]
]
]
}
我正在尝试使用 $geoInterset 运算符取回文档列表,其中 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'。 Box 会给出类似的错误。 LineString 没有抱怨,但也不会 return 任何结果。
我在这里做错了什么?有什么建议吗?
主要问题是 place
文档和查询不是正确的 Polygon GeoJSON 文档。
修复方法如下。
1)
LinearRing
的第一个和最后一个坐标必须相同。
Polygons consist of an array of GeoJSON LinearRing coordinate arrays. These LinearRings are closed LineStrings. Closed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates.
-- MongoDb doc - Polygon
2)
将 coor_type
重命名为 type
以符合 GeoJSON 标准。
{ <location field>: { type: "<GeoJSON type>" , coordinates: <coordinates> } }
In order to index GeoJSON data, you must store the data in a location field that you name. The location field contains a subdocument with a type field specifying the GeoJSON object type and a coordinates field specifying the object’s coordinates. Always store coordinates in longitude, latitude order.
Mongodb docs - geojson polygon
示例 GeoJSON 多边形文档插入:
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" })
3)
确保您的查询具有正确的多边形值,并且在包含 type
和 coordinates
字段的字段上进行搜索。
您搜索的字段 place.coordinates
,而它本应是 place
。
示例 $geoIntersects 查询
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
]
]
]
}
}
}
})
我有一个 属性 'place' 的集合,如下所示。
"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
]
]
]
}
我正在尝试使用 $geoInterset 运算符取回文档列表,其中 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'。 Box 会给出类似的错误。 LineString 没有抱怨,但也不会 return 任何结果。
我在这里做错了什么?有什么建议吗?
主要问题是 place
文档和查询不是正确的 Polygon GeoJSON 文档。
修复方法如下。
1)
LinearRing
的第一个和最后一个坐标必须相同。
Polygons consist of an array of GeoJSON LinearRing coordinate arrays. These LinearRings are closed LineStrings. Closed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates. -- MongoDb doc - Polygon
2)
将 coor_type
重命名为 type
以符合 GeoJSON 标准。
{ <location field>: { type: "<GeoJSON type>" , coordinates: <coordinates> } }
In order to index GeoJSON data, you must store the data in a location field that you name. The location field contains a subdocument with a type field specifying the GeoJSON object type and a coordinates field specifying the object’s coordinates. Always store coordinates in longitude, latitude order. Mongodb docs - geojson polygon
示例 GeoJSON 多边形文档插入:
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" })
3)
确保您的查询具有正确的多边形值,并且在包含 type
和 coordinates
字段的字段上进行搜索。
您搜索的字段 place.coordinates
,而它本应是 place
。
示例 $geoIntersects 查询
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
]
]
]
}
}
}
})