SlideShare a Scribd company logo
1 of 26
Download to read offline
GEOLOCATION IN
                                MONGODB
                             { name: ‘Shashank Tiwari’, web: ‘shanky.org’}




Wednesday, November 28, 12
WHO AM I?



                Entrepreneur, developer, author

                Most recent book: Professional NoSQL (Wiley, 2011)

                Co-founder :




Wednesday, November 28, 12
DEGREES OF MEASURE



                Latitude

                Longitude




Wednesday, November 28, 12
DEGREE OF LONGITUDE




Wednesday, November 28, 12
GEO IN MONGO




                Is a world in 2D




Wednesday, November 28, 12
MONGO DOC WITH LOC



                { loc : [37.407202, -122.10716] }

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

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




Wednesday, November 28, 12
INDEX



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

                1 geospatial index per collection




Wednesday, November 28, 12
INDEX RANGE



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




Wednesday, November 28, 12
INDEX PRECISION



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

                Geo-hash precision of 26bits ~ 2 feet




Wednesday, November 28, 12
EXACT QUERIES



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

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




Wednesday, November 28, 12
EXPLAIN QUERY PLAN




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




Wednesday, November 28, 12
COMPOUND INDEX




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




Wednesday, November 28, 12
NEAR QUERY



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

                Returns points sorted by distance




Wednesday, November 28, 12
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, November 28, 12
GEONEAR QUERY



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

                also gives the distance




Wednesday, November 28, 12
MORE WITH GEONEAR



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

                parameters: near, num, maxDistance, query




Wednesday, November 28, 12
WITHIN A BOX



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




Wednesday, November 28, 12
WITHIN A CIRCLE



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




Wednesday, November 28, 12
WITHIN A POLYGON




                Polygon shapes currently cannot be indexed




Wednesday, November 28, 12
SPHERICAL SUPPORT


                geoNear -- spherical : true

                $nearSphere

                $centerSphere

                Order is important, very important -- longitude, latitude




Wednesday, November 28, 12
MULTI LOCATIONS



                db.places.insert({ placesVisitedToday : [ { name : "San
                Antonio Caltrain", loc : [37.407202, -122.10716] }, { name :
                "Kixeye", loc : [37.7906672, -122.403256] } ] })




Wednesday, November 28, 12
QUERY MULTI LOC



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

                use uniqueDocs : true to get unique documents




Wednesday, November 28, 12
Wednesday, November 28, 12
Wednesday, November 28, 12
NEXT TALK



                More details and some about GIS too!

                SV MongoDB User Group @ Palo Alto -- January Meetup




Wednesday, November 28, 12
THANKS



                More questions? web: shanky.org | twitter : @tshanky

                Want to join the              alpha program?
                doaround.com




Wednesday, November 28, 12

More Related Content

More from MongoDB

More from MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Geolocation in mongodb

  • 1. GEOLOCATION IN MONGODB { name: ‘Shashank Tiwari’, web: ‘shanky.org’} Wednesday, November 28, 12
  • 2. WHO AM I? Entrepreneur, developer, author Most recent book: Professional NoSQL (Wiley, 2011) Co-founder : Wednesday, November 28, 12
  • 3. DEGREES OF MEASURE Latitude Longitude Wednesday, November 28, 12
  • 5. GEO IN MONGO Is a world in 2D Wednesday, November 28, 12
  • 6. MONGO DOC WITH LOC { loc : [37.407202, -122.10716] } { loc : { x: 37.407202, y: -122.10716} } { loc : { lat: 37.407202, lon: -122.10716} } Wednesday, November 28, 12
  • 7. INDEX db.places.ensureIndex( { loc : "2d" } ) 1 geospatial index per collection Wednesday, November 28, 12
  • 8. INDEX RANGE db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } ) Wednesday, November 28, 12
  • 9. INDEX PRECISION db.places.ensureIndex( { loc : "2d" } , { bits : 26 } ) Geo-hash precision of 26bits ~ 2 feet Wednesday, November 28, 12
  • 10. EXACT QUERIES > db.places.find({ loc : [ 37.407202, -122.10716 ]}); { "_id" : ObjectId("50b69e66e6945f62a019f3a4"), "name" : "San Antonio Caltrain", "loc" : [ 37.407202, -122.10716 ] } Wednesday, November 28, 12
  • 11. EXPLAIN QUERY PLAN db.places.find({ loc : [ 37.407202, -122.10716 ]}).explain(); Wednesday, November 28, 12
  • 12. COMPOUND INDEX db.places.ensureIndex( { loc : "2d", name : 1 } ) Wednesday, November 28, 12
  • 13. NEAR QUERY db.places.find({ loc : { $near : [ 37.407202, -122.10716 ]}}); Returns points sorted by distance Wednesday, November 28, 12
  • 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, November 28, 12
  • 15. GEONEAR QUERY db.runCommand({ geoNear : "places", near : [37.407202, -122.10716], num : 5 }); also gives the distance Wednesday, November 28, 12
  • 16. MORE WITH GEONEAR db.runCommand({ geoNear : "places", near : [37.407202, -122.10716], num : 5, type : “DO” }); parameters: near, num, maxDistance, query Wednesday, November 28, 12
  • 17. WITHIN A BOX db.places.find({ loc : { $within : { $box : [[39.589821, -122.438831], [37.485200, -122.228438]] }}}); Wednesday, November 28, 12
  • 18. WITHIN A CIRCLE db.places.find({ loc : { $within : { $center : [[39.589821, -122.438831], 10] }}}); Wednesday, November 28, 12
  • 19. WITHIN A POLYGON Polygon shapes currently cannot be indexed Wednesday, November 28, 12
  • 20. SPHERICAL SUPPORT geoNear -- spherical : true $nearSphere $centerSphere Order is important, very important -- longitude, latitude Wednesday, November 28, 12
  • 21. MULTI LOCATIONS db.places.insert({ placesVisitedToday : [ { name : "San Antonio Caltrain", loc : [37.407202, -122.10716] }, { name : "Kixeye", loc : [37.7906672, -122.403256] } ] }) Wednesday, November 28, 12
  • 22. QUERY MULTI LOC db.places.ensureIndex( { " placesVisitedToday.loc " : "2d"} ) use uniqueDocs : true to get unique documents Wednesday, November 28, 12
  • 25. NEXT TALK More details and some about GIS too! SV MongoDB User Group @ Palo Alto -- January Meetup Wednesday, November 28, 12
  • 26. THANKS More questions? web: shanky.org | twitter : @tshanky Want to join the alpha program? doaround.com Wednesday, November 28, 12