Mongo - GeoSpacial
#
Lesson Objectives- Explain GeoSpacial
- Explain GeoJSON
- Explain containers
- Explain How To Create an Index
- Explain $near
- Explain $geoWithin
- Explain $geoIntersects
#
Explain GeoSpacial#
Explain GeoJSON{ type: "<GeoJSON type>" , coordinates: <coordinates> }
- Always list coordinates in
longitude
,latitude
order. - point
{ type: "Point", coordinates: [ 40, 5 ] }
- line
{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }
- polygons
- array of coordinate arrays (rings)
- at least four points and have same start/end position
- one outer ring and 0+ inner rings
- cannot self intersect
- single ring
{ type: "Polygon", coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ] }
- multiple rings
- 1st ring is outer ring
- Any interior ring must be entirely contained by the outer ring
- Interior rings cannot intersect or overlap each other
- Interior rings cannot share an edge.
- http://docs.mongodb.org/manual/_images/index-2dsphere-polygon-with-ring.png
{ type: "Polygon", coordinates: [ [ [0, 0], [3, 6], [6, 1], [0, 0], ], [ [2, 2], [3, 3], [4, 2], [2, 2], ], ],};
#
Explain containers- MultiPoint
{ type: "MultiPoint", coordinates: [ [-73.958, 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.9788, 40.77247], [-73.97036, 40.76811], ], ],}
- MultiPolygon
{ 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.958, 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.9788, 40.77247], [-73.97036, 40.76811], ], ], }, ],}
#
Explain How To Create an Indexdb.collection.createIndex( { <location field> : "2dsphere" } )
#
Explain $neardb.test_data.insert({ name: "north", position: { type: "Point", coordinates: [0, 1] },});db.test_data.insert({ name: "east", position: { type: "Point", coordinates: [1, 0] },});db.test_data.insert({ name: "south", position: { type: "Point", coordinates: [0, -1] },});db.test_data.insert({ name: "west", position: { type: "Point", coordinates: [-1, 0] },});db.test_data.createIndex({ position: "2dsphere" });
db.test_data.find({ position: { $near: { $geometry: { type: "Point", coordinates: [2, 0], }, $minDistance: 0, $maxDistance: 200000, }, },});
#
Explain $geoWithindb.test_data.find({ position: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [ [ [0.5, 0.5], [1.5, 0.5], [1.5, -0.5], [0.5, -0.5], [0.5, 0.5], // VERY IMPORTANT ], ], }, }, },});
#
Explain $geoIntersectsdb.test_data.insert({ name: "line", position: { type: "LineString", coordinates: [ [1, 0], [2, 0], ], },});WriteResult({ nInserted: 1 });
db.test_data.find({ position: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [ [ [0.5, 0.5], [1.5, 0.5], [1.5, -0.5], [0.5, -0.5], [0.5, 0.5], // VERY IMPORTANT ], ], }, }, },});