Top Banner
GEOLOCATION IN MONGODB { name: ‘Shashank Tiwari’, web: ‘shanky.org’} Wednesday, January 16, 13
26

Geolocation in MongoDB

Jan 15, 2015

Download

Documents

Shashank Tiwari

A talk on "Geolocation in MongoDB" at the Silicon Valley MongoDB User Group. (http://www.meetup.com/MongoDB-SV-User-Group/events/91702142/)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Geolocation in MongoDB

GEOLOCATION IN MONGODB

{ name: ‘Shashank Tiwari’, web: ‘shanky.org’}

Wednesday, January 16, 13

Page 2: Geolocation in MongoDB

WHO AM I?

Entrepreneur, developer, author

Most recent book: Professional NoSQL (Wiley, 2011)

Founder : WhatNext Labs, creators of

Wednesday, January 16, 13

Page 3: Geolocation in MongoDB

DEGREES OF MEASURE

Latitude

Longitude

Wednesday, January 16, 13

Page 4: Geolocation in MongoDB

DEGREE OF LONGITUDE

Wednesday, January 16, 13

Page 5: Geolocation in MongoDB

GEO IN MONGO

Is a world in 2D

Wednesday, January 16, 13

Page 6: Geolocation in MongoDB

MONGO DOC WITH LOC

{ loc : [37.407202, -122.10716] }

{ loc : { x: 37.407202, y: -122.10716} }

{ loc : { lat: 37.407202, lon: -122.10716} }

Wednesday, January 16, 13

Page 7: Geolocation in MongoDB

INDEX

db.places.ensureIndex( { loc : "2d" } )

1 geospatial index per collection

Wednesday, January 16, 13

Page 8: Geolocation in MongoDB

INDEX RANGE

db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } )

Wednesday, January 16, 13

Page 9: Geolocation in MongoDB

INDEX PRECISION

db.places.ensureIndex( { loc : "2d" } , { bits : 26 } )

Geo-hash precision of 26bits ~ 2 feet

Wednesday, January 16, 13

Page 10: Geolocation in MongoDB

EXACT QUERIES

> db.places.find({ loc : [ 37.407202, -122.10716 ]});

{ "_id" : ObjectId("50b69e66e6945f62a019f3a4"), "name" : "San Antonio Caltrain", "loc" : [ 37.407202, -122.10716 ] }

Wednesday, January 16, 13

Page 11: Geolocation in MongoDB

EXPLAIN QUERY PLAN

db.places.find({ loc : [ 37.407202, -122.10716 ]}).explain();

Wednesday, January 16, 13

Page 12: Geolocation in MongoDB

COMPOUND INDEX

db.places.ensureIndex( { loc : "2d", name : 1 } )

Wednesday, January 16, 13

Page 13: Geolocation in MongoDB

NEAR QUERY

db.places.find({ loc : { $near : [ 37.407202, -122.10716 ]}});

Returns points sorted by distance

Wednesday, January 16, 13

Page 14: Geolocation in MongoDB

WITH MAXDISTANCE

db.places.find({ loc : { $near : [ 37.407202, -122.10716 ], $maxDistance : 0.05}});

What is the unit of maxDistance? -- for latitude & longitude 1 degree ~ 69 miles

Wednesday, January 16, 13

Page 15: Geolocation in MongoDB

GEONEAR QUERY

db.runCommand({ geoNear : "places", near : [37.407202, -122.10716], num : 5 });

also gives the distance

Wednesday, January 16, 13

Page 16: Geolocation in MongoDB

MORE WITH GEONEAR

db.runCommand({ geoNear : "places", near : [37.407202, -122.10716], num : 5, type : “DO” });

parameters: near, num, maxDistance, query

Wednesday, January 16, 13

Page 17: Geolocation in MongoDB

WITHIN A BOX

db.places.find({ loc : { $within : { $box : [[39.589821, -122.438831], [37.485200, -122.228438]] }}});

Wednesday, January 16, 13

Page 18: Geolocation in MongoDB

WITHIN A CIRCLE

db.places.find({ loc : { $within : { $center : [[39.589821, -122.438831], 10] }}});

Wednesday, January 16, 13

Page 19: Geolocation in MongoDB

WITHIN A POLYGON

Polygon shapes currently cannot be indexed

Wednesday, January 16, 13

Page 20: Geolocation in MongoDB

SPHERICAL SUPPORT

geoNear -- spherical : true

$nearSphere

$centerSphere

Order is important, very important -- longitude, latitude

Wednesday, January 16, 13

Page 21: Geolocation in MongoDB

MULTI LOCATIONS

db.places.insert({ placesVisitedToday : [ { name : "San Antonio Caltrain", loc : [37.407202, -122.10716] }, { name : "Datapipe", loc : [37.3336641, -121.8887072] } ] })

Wednesday, January 16, 13

Page 22: Geolocation in MongoDB

QUERY MULTI LOC

db.places.ensureIndex( { " placesVisitedToday.loc " : "2d"} )

use uniqueDocs : true to get unique documents

Wednesday, January 16, 13

Page 23: Geolocation in MongoDB

Wednesday, January 16, 13

Page 24: Geolocation in MongoDB

Wednesday, January 16, 13

Page 25: Geolocation in MongoDB

NEXT TALK

Join me to learn MongoDB in an hour!

Coming soon : Feb 1, 2013

Wednesday, January 16, 13

Page 26: Geolocation in MongoDB

THANKS

More questions? web: shanky.org | twitter : @tshanky | email: [email protected]

Download and play with on Google Play and coming soon to the AppStore.

Wednesday, January 16, 13