Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Geoindexing with MongoDB

Geoindexing with MongoDB

Leszek Krupiński

May 17, 2012
Tweet

More Decks by Leszek Krupiński

Other Decks in Programming

Transcript

  1. Is there a person at 53.438522,14.52198? Nope. Is there a

    person at 53.438522,14.52199? Nope. Is there a person at 53.438522,14.52199? Yeah, here’s Johnny!
  2. Give me nearby homies. Within the range of 1 km

    there is: • Al Gore (53.438625,14.52103) • Bill Clinton (53.432531,14.55127) • Johnny Bravo (53.438286,14.52363)
  3. SELECT c.holding_company, c.location FROM competitor c, bank b WHERE b.site_id

    = 1604 AND SDO_WITHIN_DISTANCE(c.location, b.location, ’distance=2 unit=mile’) = ’TRUE’ ORACLE
  4. Why MongoDB? Use Cases • Archiving • Event logging •

    Document and CMS • Gaming • High volume sites • Mobile • Operational datastore • Agile development • Real-time stats Features • Ad hoc queries • Indexing • Replication • Load Balancing • File Storage • Aggregation • Server-side JavaScript • Capped collections http://en.wikipedia.org/wiki/Mongodb
  5. Query • Exact o db.places.find( { loc : [50,50] }

    ) • Near o db.places.find( { loc : { $near : [50,50] } } ) • Limit o db.places.find( { loc : { $near : [50,50] } } ).limit(20) • Distance o db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } ).limit(20)
  6. Compound index • db.places.ensureIndex( { location : "2d" , category

    : 1 } ); • db.places.find( { location : { $near : [50,50] }, category : 'coffee‚ } );
  7. Bound queries • box = [ [40.73083, -73.99756], [40.741404, -73.988135]

    ] • db.places.find( {"loc" : {"$within" : {"$box" : box }} } )
  8. earthRadius = 6378 // km multi = earthRadius * PI

    / 180.0 range = 3000 // km … maxDistance : range * multi…
  9. MongoDB has it built-in distances = db.runCommand( { geoNear :

    "points", near : [0, 0], spherical : true, maxDistance : range / earthRadius /* to radians */ } ).results
  10. Automatically sorted • db.runCommand( { geoNear : "places" , near

    : [50,50], num : 10 } ); • { "ns" : "test.places", "results" : [ { "dis" : 69.29646421910687, "obj" : … }, { "dis" : 69.29646421910687, "obj" : … }, … ], … }
  11. Why MongoDB? Use Cases • Archiving • Event logging •

    Document and CMS • Gaming • High volume sites • Mobile • Operational datastore • Agile development • Real-time stats Features • Ad hoc queries • Indexing • Replication • Load Balancing • File Storage • Aggregation • Server-side JavaScript • Capped collections http://en.wikipedia.org/wiki/Mongodb