27
MongoDB GeoSpatial Feature Hüseyin BABAL @huseyinbabal

MongoDB GeoSpatial Feature

Embed Size (px)

Citation preview

MongoDB GeoSpatial Feature

Hüseyin BABAL@huseyinbabal

What is MongoDB GeoSpatial Feature?

MongoDB allows you to create index and queries to make operations on geospatial

(location information) data

In order to define index, or execute query, you need to determine surface

first

Surfaces

Spherical

To calculate geometry over an Earth-like

sphere, store your location data on a

spherical surface and use 2dsphere

index.

MongoDB Reference

Store your location data as GeoJSON objects with this coordinate-axis order: longitude, latitude. The coordinate reference system for GeoJSON uses the WGS84 datum.

MongoDB Reference

GeoJSON

Example Usage

Example Usage

Flat

To calculate distances on a Euclidean plane, store your location data as legacy coordinate pairs and use a 2d index.

MongoDB Reference

Query Operations

Inclusion

MongoDB can query for locations contained entirely within a specified polygon. Inclusion queries use the $geoWithin operator. No index required, 2d and 2dsphere supports

MongoDB Reference

Intersection

MongoDB can query for locations that intersect with a specified geometry. These queries apply only to data on a spherical surface. These queries use the $geoIntersects operator. Index required, only 2dsphere supports

MongoDB Reference

Proximity

MongoDB can query for the points nearest to another point. Proximity queries use the $near operator. 2d or 2dsphere required

MongoDB Reference

Index Types

2d

● Calculations using flat geometry

● Legacy coordinate pairs (i.e.,

geospatial points on a flat coordinate

system)

● Compound indexes with only one

additional field, as a suffix of the 2d

index field

2d Index Creation

db.collection.createIndex( { <location field> : "2d" } ,

{ min : <lower bound> , max : <upper bound> } )

db.collection.createIndex( { loc : "2d" } ,

{ min : -45 , max : 45 } )

2d Query

db.<collection>.find( { <location field> :

{ $geoWithin :

{ $box|$polygon|$center : <coordinates>

} } } )

db.places.find( { loc :

{ $geoWithin :

{ $box : [ [ 0 , 0 ] ,

[ 100 , 100 ] ]

} } } )

db.places.find( { loc: { $geoWithin :

{ $center : [ [-74, 40.74 ] , 10 ]

} } } )

db.<collection>.find( { <location field> :

{ $near : [ <x> , <y> ]

} } )

db.<collection>.find( { loc: [ <x> , <y> ] } )

2dsphere Index Creation

db.collection.createIndex( { <location field> : "2dsphere" } )

db.collection.createIndex( { loc : "2dsphere" } )

2dsphere Query

db.<collection>.find( { <location field> :

{ $geoWithin :

{ $geometry :

{ type : "Polygon" ,

coordinates : [ <coordinates> ]

} } } } )

db.places.find( { loc :

{ $geoWithin :

{ $geometry :

{ type : "Polygon" ,

coordinates : [ [[ 0 , 0 ] ,[ 3 , 6 ] ,[ 6 , 1 ] ,[ 0 , 0 ]] ]} } } } )

Intersection

db.<collection>.find( { <location field> :

{ $geoIntersects :

{ $geometry :

{ type : "<GeoJSON object type>" ,

coordinates : [ <coordinates> ]

} } } } )

db.places.find( { loc :

{ $geoIntersects :

{ $geometry :

{ type : "Polygon" ,

coordinates: [ [[ 0 , 0 ] ,[ 3 , 6 ] ,[ 6 , 1 ] ,[ 0 , 0 ]] ]} } } } )

Proximity

db.<collection>.find( { <location field> :

{ $near :

{ $geometry :

{ type : "Point" ,

coordinates : [ <longitude> , <latitude> ] } ,

$maxDistance : <distance in meters>

} } } )

2dsphere

● Calculations on a sphere

● GeoJSON objects and include

backwards compatibility for legacy

coordinate pairs

● Compound indexes with scalar index

fields (i.e. ascending or descending) as

a prefix or suffix of the 2dsphere index

field

geoHaystack

An index is a special index that is

optimized to return results over small

areas. geoHaystack indexes improve

performance on queries that use flat

geometry.

Any Questions