Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Geoindexing with MongoDB
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Leszek Krupiński
May 17, 2012
Programming
61
0
Share
Geoindexing with MongoDB
Leszek Krupiński
May 17, 2012
More Decks by Leszek Krupiński
See All by Leszek Krupiński
So that the daemon won’t die
leafnode
2
410
Practical PHP7
leafnode
2
490
Dobrze posól swoje hasło
leafnode
0
120
Dobrze posól swoje hasło (z notatkami)
leafnode
0
110
PHPNG kontra HHVM
leafnode
0
120
PHPNG kontra HHVM (z notatkami)
leafnode
0
86
Ewolucja PHP: PHP 5.6, NG, PHP 7, HHVM
leafnode
2
310
Sculpin - Generowanie statycznych stron w PHP
leafnode
2
77
Skalowanie aplikacji PHP
leafnode
1
430
Other Decks in Programming
See All in Programming
20260514 - build with ai 2026 - build LINE Bot with Gemini CLI
line_developers_tw
PRO
0
310
KMP × Kotlin 2.3 - How Android Got Slower While iOS Builds Improved by 47%
rio432
0
130
サプライチェーン攻撃対策「層を重ねて落ちない壁」を10日間で組み上げた話 #TechLeadConf2026
kashewnuts
1
210
空間オーディオの活用
objectiveaudio
0
140
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
1.6k
Are We Really Coding 10× Faster with AI?
kohzas
0
120
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
480
The Less-Told Story of Socket Timeouts
coe401_
3
980
SREに優しいTerraform構成 modulesとstateの組み方
hiyanger
2
170
いつか誰かが、と思っていた フロントエンド刷新5年間の実践知
kiichisugihara
1
260
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
730
Structured Concurrency, Scoped Values and Joiners in the JDK 25 26 27
josepaumard
1
140
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
AI: The stuff that nobody shows you
jnunemaker
PRO
6
630
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
740
The Curious Case for Waylosing
cassininazir
0
340
Optimizing for Happiness
mojombo
378
71k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
290
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
350
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.5k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.7k
Transcript
Geoindexing with MongoDB Leszek Krupiński WebClusters 2012
About me
On-line since 1997
Funny times
1 hr of internet for 1 USD
None
None
First social site: geocities
My first web page
What do I do now
Day-time job Managing team of developers for Polish Air Force
Side: consulting, optimizing, desiging
Buzzwords incoming!
The Internet 2008
Web 2.0
http://en.wikipedia.org/wiki/File:Web_2.0_Map.svg CC-BY-SA-2.5
Be social in your bedroom
alone.
The Internet 2012
Web 3.0
None
Why geospatial?
Needs shifted
Why? Because they could.
None
None
None
How to implement?
Database. Duh.
Keep, but also query
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!
Not too useful.
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)
Now that’s better.
Geoindexing. Nothing new.
Oracle, PostreSQL, Lucene/Solr, even MySQL (via extensions)
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
SQL is so last year
Let’s use something cool
MongoDB. Because all the cool kids use NoSQL now
None
Why MongoDB?
Choose your NoSQL wise.
NoSQL in MongoDB • Document –based • Queries (JS-like syntax)
• JSON-like storage
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
Back to geo.
{ loc: [ 52.0, 21.0 ], name: ”Warsaw”, type: ”City”
}
db.nodes.ensureIndex({loc: '2d'})
That’s it.
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)
Compound index • db.places.ensureIndex( { location : "2d" , category
: 1 } ); • db.places.find( { location : { $near : [50,50] }, category : 'coffee‚ } );
Bound queries • box = [ [40.73083, -73.99756], [40.741404, -73.988135]
] • db.places.find( {"loc" : {"$within" : {"$box" : box }} } )
Problems
Units
Coordinates in arc units Distance in kilometers
In query
earthRadius = 6378 // km multi = earthRadius * PI
/ 180.0 range = 3000 // km … maxDistance : range * multi…
In results
pointDistance = distances[0].dis / multi
Earth is not flat.
Problem: can’t use linear distance
Earth isn’t flat too.
Solution? Use approximation.
MongoDB has it built-in distances = db.runCommand( { geoNear :
"points", near : [0, 0], spherical : true, maxDistance : range / earthRadius /* to radians */ } ).results
Focus: runCommand distances = db.runCommand({ geoNear : "points" …
Sort by distance Only with runCommand
Automatically sorted • db.runCommand( { geoNear : "places" , near
: [50,50], num : 10 } ); • { "ns" : "test.places", "results" : [ { "dis" : 69.29646421910687, "obj" : … }, { "dis" : 69.29646421910687, "obj" : … }, … ], … }
Demo
OpenStreetMaps database of Poland imported into MongoDB
14.411.552 nodes
3GB of raw XML data
PHP in virtual machine
Imported about 100.000 nodes every 10s.
Pretty cool, eh?
Kudos to Derick Rethans Part of this talk was inspired
by his talk
Questions?
Thanks! Rate me at https://joind.in/talk/view/6475
Geoindexing with MongoDB supplement Leszek Krupiński WebClusters 2012
Why MongoDB?
Evaluate.
PostGIS is cool too. (but it’s SQL, meh)
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
If you need other features of MongoDB, use it
If you don’t, evaluate.
Evaluate.
Demo (hopefully)
Questions?
Please leave feedback! Rate me at https://joind.in/6475