1. GEOLOCATION IN
MONGODB
{ name: ‘Shashank Tiwari’, web: ‘shanky.org’}
Wednesday, January 16, 13
2. WHO AM I?
Entrepreneur, developer, author
Most recent book: Professional NoSQL (Wiley, 2011)
Founder : WhatNext Labs, creators of
Wednesday, January 16, 13
11. EXPLAIN QUERY PLAN
db.places.find({ loc : [ 37.407202, -122.10716 ]}).explain();
Wednesday, January 16, 13
12. COMPOUND INDEX
db.places.ensureIndex( { loc : "2d", name : 1 } )
Wednesday, January 16, 13
13. NEAR QUERY
db.places.find({ loc : { $near : [ 37.407202, -122.10716 ]}});
Returns points sorted by distance
Wednesday, January 16, 13
14. 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
15. GEONEAR QUERY
db.runCommand({ geoNear : "places", near : [37.407202,
-122.10716], num : 5 });
also gives the distance
Wednesday, January 16, 13
16. 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
17. WITHIN A BOX
db.places.find({ loc : { $within : { $box : [[39.589821,
-122.438831], [37.485200, -122.228438]] }}});
Wednesday, January 16, 13
18. WITHIN A CIRCLE
db.places.find({ loc : { $within : { $center : [[39.589821,
-122.438831], 10] }}});
Wednesday, January 16, 13
19. WITHIN A POLYGON
Polygon shapes currently cannot be indexed
Wednesday, January 16, 13
20. SPHERICAL SUPPORT
geoNear -- spherical : true
$nearSphere
$centerSphere
Order is important, very important -- longitude, latitude
Wednesday, January 16, 13
21. 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
22. QUERY MULTI LOC
db.places.ensureIndex( { " placesVisitedToday.loc " : "2d"} )
use uniqueDocs : true to get unique documents
Wednesday, January 16, 13
25. NEXT TALK
Join me to learn MongoDB in an hour!
Coming soon : Feb 1, 2013
Wednesday, January 16, 13
26. THANKS
More questions? web: shanky.org | twitter : @tshanky | email:
st@treasuryofideas.com
Download and play with on Google Play and
coming soon to the AppStore.
Wednesday, January 16, 13