与MySQL类似,Mongo也支持空间地理位置的类型Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon、GeometryCollection。
Mongo有集合类型GeometryCollection,但是没有一个可以指代任意(Point、LineString、Polygon)的Geometry。
基础
MongoDB对GeoJSON对象的地理空间查询是在一个球体上计算,使用WGS84参考系统。而在MySQL中默认是笛卡尔平面,也可以指定SRID属性为4326指明WGS84参考系。目前没看到Mongo可以另外指定参考系的资料。
Mongo的各类型的定义也非常简单,不像MySQL还需要使用ST_GeomFromText。其定义的语法:
{ type: <GeoJSON type> , coordinates: <coordinates> }
Mongo GeoJSON具体类型
Point
{type: "Point",coordinates: [-73.856077, 40.848447]}
LineString
{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }
Polygon
{
type: "Polygon",
coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
}
包含多个边界的Polygon
{
type : "Polygon",
coordinates : [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
]
}
MultiPoint
{
type: "MultiPoint",
coordinates: [
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
}
MultiLineString
{
type: "MultiLineString",
coordinates: [
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
]
}
MultiPolygon
注意与包含多个边界的Polygon的区别
{
type: "MultiPolygon",
coordinates: [
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
]
}
GeometryCollection
{
type: "GeometryCollection",
geometries: [
{
type: "MultiPoint",
coordinates: [
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
},
{
type: "MultiLineString",
coordinates: [
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
]
}
]
}
Mongo GeoJSON的索引
Mongo中的索引有两种2dsphere和2d,2dsphere支持球面几何的查询而2d仅支持二维平面几何的查询;
索引的创建比较简单:
db.collection.createIndex( { <location field> : "2dsphere" } )
db.collection.createIndex( { <location field> : "2d" } )
MySQL的索引只有SPATIAL INDEX,但是可以用不同的函数来进行不同的操作,比如ST_Distance() 、ST_Distance_Sphere() 、ST_HausdorffDistance() 等。
Mongo GeoJSON查询操作
主要的查询操作有
- $geoIntersects,几何相交,注意只支持2dsphere索引类型
- $geoWithin
- $near
- $nearSphere
配合各种Geometry Specifiers,Mongo可以做到各式各样的查询。具体参见query-geospatial
MySQL也提供了一大批函数来支持各种查询,比如ST_Intersects()、ST_Within()、ST_Distance_Sphere()、ST_Overlaps() 、ST_Contains() 等等。列表参见spatial-function-reference
MySQL和Mongo的查询都是非常丰富的。
MySQL 转GeoJSON
MySQL中提供了ST_AsGeoJSON,可以将几何/地理数据类型转成GeoJSON。
如下,其中2指精度,四舍五入
SELECT ST_AsGeoJSON(ST_GeomFromText('POINT(11.11111 12.22222)'),2);
-- {"type": "Point", "coordinates": [11.11, 12.22]}
再比如:
SELECT ST_AsGeoJSON(ST_GeomFromText('GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'),2);
输出如下:
{"type": "GeometryCollection", "geometries": [{"type": "Point", "coordinates": [1.0, 1.0]}, {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]]}]}
MySQL提供了将GeoJSON转几何/地理数据类型的函数ST_GeomFromGeoJSON
Mongo的 legacy coordinate pairs
除了GeoJSON,Mongo还支持传统的坐标对的方式,其定义方式是:
<field>: [<longitude>, <latitude> ]
或
<field>: { <field1>: <longitude>, <field2>: <latitude> }
这种方式也可以使用平面的2d索引