SlideShare une entreprise Scribd logo
1  sur  33
Mapping Flatland
   Storing and Querying Location
   Data with MongoDB



MongoSF 2011 (#mongosf)            Grant Goodale (@ggoodale)
May 24, 2011
Flatland
Flatland?
A sense of scale
The world
A bit of history

Originally written for Node Knockout 2010
Why MongoDB?


Easy to set up and run
Fast
Reasonably robust node.js driver (even better now)
Geospatial indexing and querying
Geo2D in 1.8

Data and Indices
Querying
Running / Scaling Production
Structuring your data
tile = {
    _id        :   BSON::ObjectId(...)
    position   :   [0,0],
    letter     :   "A",
    wildcard   :   "false"
}
Structuring your data
tile = {
    _id        :   BSON::ObjectId(...)
    position   :   {x: 0, y: 0},
    letter     :   "A",
    wildcard   :   "false"
}
Watch your language
> db[‘tiles’].insert({
    position : {y: 50, x: 20},
    letter   : "A",
    wildcard : "false"
})
=> BSON::ObjectId('4dd06d037a70183256000004')
> db.[‘tiles’].find_one()
=>
{"_id"=>BSON::ObjectId('4dd06d037a70183256000
004'), "letter"=>"A", "position"=>{"x"=>20,
"y"=>50}, "wildcard"=>false}
Be safe!

Use array notation; guaranteed ordering = WIN
Ruby: Use 1.9.x or OrderedHash in 1.8.x
Python: Use OrderedDict (introduced in 2.7) and SON
(in the BSON package)
Javascript: Did I mention arrays?
Creating the index
> db[‘tiles’].create_index([[“position”,
                              Mongo::GEO2D]])
=> “position_2d”
> db[‘tiles’].index_information
=> {"_id_"=>{"name"=>"_id_",
"ns"=>"test.test_tiles", "key"=>{"_id"=>1}},
"position_2d"=>{"key"=>{"position"=>"2d"},
"ns"=>"test.test_tiles", "name"=>"position_2d"}}


 Defaults:
   Min: -180, Max: 180,
   bits: 26
Creating the index
> db[‘tiles’].create_index(
    [[“position”, Mongo::GEO2D]],
    :min => -500, :max => 500, :bits => 32
  )
=> “position_2d”
More index fun

 Only one Geo2D index per collection (SERVER-2331)
 But it can be a compound index:

> db[‘tiles’].create_index([
     [“position”, Mongo::GEO2D],
     [“letter”, Mongo::ASCENDING]
  ])
=> “position_2d_letter_1”
1.9.x bonus feature
          Geo2d indices across an array field!
> db[‘words’].insert({
     “word” : “QI”,
     “tiles” : [
       {“letter” => “Q”, position => [1,1]},
       {“letter” => “I”, position => [2,1]}
     ]
  })
=> BSON::ObjectID('4dd074927a70183256000006')
> db[‘words’].create_index([[
     “tiles.position”,
    Mongo::GEO2D
  ]])
=> “position_2d”
Geo2D in 1.8

Data and Indices
Querying
Running / Scaling Production
A caveat: We’re weird.


                                                  VS


http://www.flickr.com/photos/toasty/1540997910/        http://www.flickr.com/photos/vizzzual-dot-com/2175221420/
A caveat: We’re weird.


          VS
Problems we don’t have
Projection issues
Great Circle distance
calculation
Polar coordinate systems
Pirates
                           http://www.flickr.com/photos/jmd41280/4501410061/
Querying real location data
  Search by proximity: $near
  Uses native units (degrees for [-180, 180])
  Use $maxDistance to bound query

> db[‘tile’].find(:position => {“$near” => [10,10]}).to_a
=> [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'),
"letter"=>"A", "position"=>[12,9]}]

> db[‘tile’].find(:position => {“$near” => [10,10],
“$maxDistance” => 1}).to_a
=>[]
Querying real location data
  Need distance to center as well? Use $geoNear
  Also includes fun stats
> db.command('geoNear' => 'tiles', 'near' => [1830,
2002], :maxDistance => 10)
)
=> {"ns"=>"test.tiles",
"near"=>"110000000000001100011000110010101010001000001011
1111", "results"=>[{"dis"=>3.999471664428711,
"obj"=>{"_id"=>BSON::ObjectId('4dd0b0957a701852bc02bf67')
, "position"=>{"x"=>1830, "y"=>2006}, "letter"=>"A"}}],
"stats"=>{"time"=>0, "btreelocs"=>3, "nscanned"=>2,
"objectsLoaded"=>1, "avgDistance"=>3.999471664428711,
"maxDistance"=>3.999471664428711}, "ok"=>1.0}
Querying real location data
 Region queries: $within
 Example: $box (rectangle)
> db[‘tile’].find(:position => {“$within” => {“$box” =>
[[10,10], [30,30]]}).to_a
=> [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'),
"letter"=>"A", "position"=>[12,9]}]




                              [30,30]




                    [10,10]
Querying real location data
 Alternately: $center (circle)

> db[‘tile’].find(:position => {“$within” => {“$center”
=> [[10,10], 5]}).to_a
=> [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'),
"letter"=>"A", "position"=>[12,9]}]




                                5
                          [10,10]
Querying real location data
 1.9.x bonus feature: $polygon!

> db[‘tile’].find(:position => {“$within” => {“$polygon”
=> [[5,5], [5,15], [15,5]}).to_a
=> [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'),
"letter"=>"A", "position"=>[12,9]}]



                        [5,15]




                        [5,5]    [15,5]
Querying real location data
  Spherical equivalents: $nearSphere and $centerSphere
  Uses radians, not native units
  position must be in [long, lat] order!
> earthRadius = 6378 #km
=> 6378
> db[‘restaurants’].find(:position => {“$nearSphere” =>
[-122.03,36.97], “$maxDistance” => 25.0/earthRadius}).to_a
=> [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'),
"name"=>"Crow’s Nest", "position"=>[-122.0,36.96]}]
MapReduce

using Geo2D in MapReduce and group queries doesn’t
work (SERVER-1742)
Fixed in 1.8.2 (Yay Greg!)
Geo2D in 1.8

Data and Indices
Querying
Running / Scaling Production
Again: we’re weird.

Big index, but no need for it all to be in memory
Large regions of the world where activity => 0 as
density => 1
Single box scaling limit determined by # of active
sections of the world at a time
Our setup
Master/Slave, moving to Replica Set
Slaves used for
  backup
  Map image generation
Next stop: geoSharding
Sharding

Yes, you can shard on a geo-indexed field
Not recommended in 1.8 due to query performance
(SERVER-1982). Fix coming in 1.9.x.
Can’t use $near in queries, only $geoNear and
therefore runCommand(). (SERVER-1981)
Questions?




               http://www.flickr.com/photos/wili/3361117222/



@ggoodale
grant@massivelyfun.com

Contenu connexe

Tendances

Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
Internet of Information and Services (IoIS): A Conceptual Integrative Archite...
Internet of Information and Services (IoIS): A Conceptual Integrative Archite...Internet of Information and Services (IoIS): A Conceptual Integrative Archite...
Internet of Information and Services (IoIS): A Conceptual Integrative Archite...Antonio Marcos Alberti
 
2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamsky2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamskyData Con LA
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesMongoDB
 
Jan Lehnardt Couch Db In A Real World Setting
Jan Lehnardt Couch Db In A Real World SettingJan Lehnardt Couch Db In A Real World Setting
Jan Lehnardt Couch Db In A Real World SettingGeorge Ang
 
Android and firebase database
Android and firebase databaseAndroid and firebase database
Android and firebase databaseNILESH SAWARDEKAR
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
Building a Social Network with MongoDB
  Building a Social Network with MongoDB  Building a Social Network with MongoDB
Building a Social Network with MongoDBFred Chu
 
Montreal Sql saturday: moving data from no sql db to azure data lake
Montreal Sql saturday: moving data from no sql db to azure data lakeMontreal Sql saturday: moving data from no sql db to azure data lake
Montreal Sql saturday: moving data from no sql db to azure data lakeDiponkar Paul
 
Building DSLs with Groovy
Building DSLs with GroovyBuilding DSLs with Groovy
Building DSLs with GroovySten Anderson
 
MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010Eliot Horowitz
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance TuningPuneet Behl
 
Optimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityOptimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityMongoDB
 
Basic crud operation
Basic crud operationBasic crud operation
Basic crud operationzarigatongy
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL SpartakiadeJohannes Hoppe
 
MongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDBMongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDBMongoDB
 
MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentialszahid-mian
 
Advanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDBAdvanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDBJohn De Goes
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)Johannes Hoppe
 

Tendances (20)

Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Internet of Information and Services (IoIS): A Conceptual Integrative Archite...
Internet of Information and Services (IoIS): A Conceptual Integrative Archite...Internet of Information and Services (IoIS): A Conceptual Integrative Archite...
Internet of Information and Services (IoIS): A Conceptual Integrative Archite...
 
2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamsky2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamsky
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
 
Jan Lehnardt Couch Db In A Real World Setting
Jan Lehnardt Couch Db In A Real World SettingJan Lehnardt Couch Db In A Real World Setting
Jan Lehnardt Couch Db In A Real World Setting
 
Android and firebase database
Android and firebase databaseAndroid and firebase database
Android and firebase database
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
Building a Social Network with MongoDB
  Building a Social Network with MongoDB  Building a Social Network with MongoDB
Building a Social Network with MongoDB
 
Montreal Sql saturday: moving data from no sql db to azure data lake
Montreal Sql saturday: moving data from no sql db to azure data lakeMontreal Sql saturday: moving data from no sql db to azure data lake
Montreal Sql saturday: moving data from no sql db to azure data lake
 
Building DSLs with Groovy
Building DSLs with GroovyBuilding DSLs with Groovy
Building DSLs with Groovy
 
MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010
 
MongoDB Performance Tuning
MongoDB Performance TuningMongoDB Performance Tuning
MongoDB Performance Tuning
 
Optimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityOptimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and Creativity
 
Basic crud operation
Basic crud operationBasic crud operation
Basic crud operation
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade
 
MongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDBMongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB .local London 2019: Tips and Tricks++ for Querying and Indexing MongoDB
 
NoSQL - Hands on
NoSQL - Hands onNoSQL - Hands on
NoSQL - Hands on
 
MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentials
 
Advanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDBAdvanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDB
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
 

En vedette

Building for Real Time
Building for Real TimeBuilding for Real Time
Building for Real TimeGrant Goodale
 
Seattle javascript game development - Overview
Seattle javascript game development - OverviewSeattle javascript game development - Overview
Seattle javascript game development - OverviewGrant Goodale
 
Catan world and Churchill
Catan world and ChurchillCatan world and Churchill
Catan world and ChurchillGrant Goodale
 
#CPBR7 - Métricas para startups
#CPBR7 - Métricas para startups#CPBR7 - Métricas para startups
#CPBR7 - Métricas para startupsSaulo Arruda
 
Node js mongodriver
Node js mongodriverNode js mongodriver
Node js mongodriverchristkv
 
Conhecendo as Opcoes de Bancos de Dados na Nuvem da AWS
Conhecendo as Opcoes de Bancos de Dados na Nuvem da AWSConhecendo as Opcoes de Bancos de Dados na Nuvem da AWS
Conhecendo as Opcoes de Bancos de Dados na Nuvem da AWSAmazon Web Services LATAM
 
Developing Configurable and High Performance Apps in Drools
Developing Configurable and High Performance Apps in Drools Developing Configurable and High Performance Apps in Drools
Developing Configurable and High Performance Apps in Drools Ajay Mahajan
 
Continuous delivery - tools and techniques
Continuous delivery - tools and techniquesContinuous delivery - tools and techniques
Continuous delivery - tools and techniquesMike McGarr
 
Java Tools and Technologies Landscape for 2014 (image gallery)
Java Tools and Technologies Landscape for 2014 (image gallery)Java Tools and Technologies Landscape for 2014 (image gallery)
Java Tools and Technologies Landscape for 2014 (image gallery)ZeroTurnaround
 
Hybrid MongoDB and RDBMS Applications
Hybrid MongoDB and RDBMS ApplicationsHybrid MongoDB and RDBMS Applications
Hybrid MongoDB and RDBMS ApplicationsSteven Francia
 

En vedette (12)

Building for Real Time
Building for Real TimeBuilding for Real Time
Building for Real Time
 
Seattle javascript game development - Overview
Seattle javascript game development - OverviewSeattle javascript game development - Overview
Seattle javascript game development - Overview
 
Catan world and Churchill
Catan world and ChurchillCatan world and Churchill
Catan world and Churchill
 
#CPBR7 - Métricas para startups
#CPBR7 - Métricas para startups#CPBR7 - Métricas para startups
#CPBR7 - Métricas para startups
 
Melhores Práticas no Uso da Nuvem AWS
Melhores Práticas no Uso da Nuvem AWSMelhores Práticas no Uso da Nuvem AWS
Melhores Práticas no Uso da Nuvem AWS
 
Node js mongodriver
Node js mongodriverNode js mongodriver
Node js mongodriver
 
Arquiteturas de Alta Disponibilidade na AWS
Arquiteturas de Alta Disponibilidade na AWSArquiteturas de Alta Disponibilidade na AWS
Arquiteturas de Alta Disponibilidade na AWS
 
Conhecendo as Opcoes de Bancos de Dados na Nuvem da AWS
Conhecendo as Opcoes de Bancos de Dados na Nuvem da AWSConhecendo as Opcoes de Bancos de Dados na Nuvem da AWS
Conhecendo as Opcoes de Bancos de Dados na Nuvem da AWS
 
Developing Configurable and High Performance Apps in Drools
Developing Configurable and High Performance Apps in Drools Developing Configurable and High Performance Apps in Drools
Developing Configurable and High Performance Apps in Drools
 
Continuous delivery - tools and techniques
Continuous delivery - tools and techniquesContinuous delivery - tools and techniques
Continuous delivery - tools and techniques
 
Java Tools and Technologies Landscape for 2014 (image gallery)
Java Tools and Technologies Landscape for 2014 (image gallery)Java Tools and Technologies Landscape for 2014 (image gallery)
Java Tools and Technologies Landscape for 2014 (image gallery)
 
Hybrid MongoDB and RDBMS Applications
Hybrid MongoDB and RDBMS ApplicationsHybrid MongoDB and RDBMS Applications
Hybrid MongoDB and RDBMS Applications
 

Similaire à Geospatial Indexing and Querying with MongoDB

MongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureMongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureHüseyin BABAL
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2Takahiro Inoue
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDBTakahiro Inoue
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)MongoSF
 
Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNosh Petigara
 
Building Applications with MongoDB - an Introduction
Building Applications with MongoDB - an IntroductionBuilding Applications with MongoDB - an Introduction
Building Applications with MongoDB - an IntroductionMongoDB
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONTomomi Imura
 
MongoDB for Analytics
MongoDB for AnalyticsMongoDB for Analytics
MongoDB for AnalyticsMongoDB
 
Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016
Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016
Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016Luigi Dell'Aquila
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreamsguest35660bc
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time GeostreamsRaffi Krikorian
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsAlexander Rubin
 
Creating a Single View: Data Design and Loading Strategies
Creating a Single View: Data Design and Loading StrategiesCreating a Single View: Data Design and Loading Strategies
Creating a Single View: Data Design and Loading StrategiesMongoDB
 
OrientDB - The 2nd generation of (multi-model) NoSQL
OrientDB - The 2nd generation of  (multi-model) NoSQLOrientDB - The 2nd generation of  (multi-model) NoSQL
OrientDB - The 2nd generation of (multi-model) NoSQLRoberto Franchini
 
Nosh slides mongodb web application - mongo philly 2011
Nosh slides   mongodb web application - mongo philly 2011Nosh slides   mongodb web application - mongo philly 2011
Nosh slides mongodb web application - mongo philly 2011MongoDB
 
Casting for not so strange actors
Casting for not so strange actorsCasting for not so strange actors
Casting for not so strange actorszucaritask
 
SQL Geography Datatypes by Jared Nielsen and the FUZION Agency
SQL Geography Datatypes by Jared Nielsen and the FUZION AgencySQL Geography Datatypes by Jared Nielsen and the FUZION Agency
SQL Geography Datatypes by Jared Nielsen and the FUZION AgencyJared Nielsen
 

Similaire à Geospatial Indexing and Querying with MongoDB (20)

MongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureMongoDB GeoSpatial Feature
MongoDB GeoSpatial Feature
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDB
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
 
Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Building Applications with MongoDB - an Introduction
Building Applications with MongoDB - an IntroductionBuilding Applications with MongoDB - an Introduction
Building Applications with MongoDB - an Introduction
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
MongoDB for Analytics
MongoDB for AnalyticsMongoDB for Analytics
MongoDB for Analytics
 
Mobile Web 5.0
Mobile Web 5.0Mobile Web 5.0
Mobile Web 5.0
 
Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016
Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016
Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreams
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreams
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of Things
 
Creating a Single View: Data Design and Loading Strategies
Creating a Single View: Data Design and Loading StrategiesCreating a Single View: Data Design and Loading Strategies
Creating a Single View: Data Design and Loading Strategies
 
OrientDB - The 2nd generation of (multi-model) NoSQL
OrientDB - The 2nd generation of  (multi-model) NoSQLOrientDB - The 2nd generation of  (multi-model) NoSQL
OrientDB - The 2nd generation of (multi-model) NoSQL
 
Nosh slides mongodb web application - mongo philly 2011
Nosh slides   mongodb web application - mongo philly 2011Nosh slides   mongodb web application - mongo philly 2011
Nosh slides mongodb web application - mongo philly 2011
 
Casting for not so strange actors
Casting for not so strange actorsCasting for not so strange actors
Casting for not so strange actors
 
MongoDB With Style
MongoDB With StyleMongoDB With Style
MongoDB With Style
 
SQL Geography Datatypes by Jared Nielsen and the FUZION Agency
SQL Geography Datatypes by Jared Nielsen and the FUZION AgencySQL Geography Datatypes by Jared Nielsen and the FUZION Agency
SQL Geography Datatypes by Jared Nielsen and the FUZION Agency
 

Dernier

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Dernier (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Geospatial Indexing and Querying with MongoDB

  • 1. Mapping Flatland Storing and Querying Location Data with MongoDB MongoSF 2011 (#mongosf) Grant Goodale (@ggoodale) May 24, 2011
  • 4.
  • 5. A sense of scale
  • 7. A bit of history Originally written for Node Knockout 2010
  • 8. Why MongoDB? Easy to set up and run Fast Reasonably robust node.js driver (even better now) Geospatial indexing and querying
  • 9. Geo2D in 1.8 Data and Indices Querying Running / Scaling Production
  • 10. Structuring your data tile = { _id : BSON::ObjectId(...) position : [0,0], letter : "A", wildcard : "false" }
  • 11. Structuring your data tile = { _id : BSON::ObjectId(...) position : {x: 0, y: 0}, letter : "A", wildcard : "false" }
  • 12. Watch your language > db[‘tiles’].insert({ position : {y: 50, x: 20}, letter : "A", wildcard : "false" }) => BSON::ObjectId('4dd06d037a70183256000004') > db.[‘tiles’].find_one() => {"_id"=>BSON::ObjectId('4dd06d037a70183256000 004'), "letter"=>"A", "position"=>{"x"=>20, "y"=>50}, "wildcard"=>false}
  • 13. Be safe! Use array notation; guaranteed ordering = WIN Ruby: Use 1.9.x or OrderedHash in 1.8.x Python: Use OrderedDict (introduced in 2.7) and SON (in the BSON package) Javascript: Did I mention arrays?
  • 14. Creating the index > db[‘tiles’].create_index([[“position”, Mongo::GEO2D]]) => “position_2d” > db[‘tiles’].index_information => {"_id_"=>{"name"=>"_id_", "ns"=>"test.test_tiles", "key"=>{"_id"=>1}}, "position_2d"=>{"key"=>{"position"=>"2d"}, "ns"=>"test.test_tiles", "name"=>"position_2d"}} Defaults: Min: -180, Max: 180, bits: 26
  • 15. Creating the index > db[‘tiles’].create_index( [[“position”, Mongo::GEO2D]], :min => -500, :max => 500, :bits => 32 ) => “position_2d”
  • 16. More index fun Only one Geo2D index per collection (SERVER-2331) But it can be a compound index: > db[‘tiles’].create_index([ [“position”, Mongo::GEO2D], [“letter”, Mongo::ASCENDING] ]) => “position_2d_letter_1”
  • 17. 1.9.x bonus feature Geo2d indices across an array field! > db[‘words’].insert({ “word” : “QI”, “tiles” : [ {“letter” => “Q”, position => [1,1]}, {“letter” => “I”, position => [2,1]} ] }) => BSON::ObjectID('4dd074927a70183256000006') > db[‘words’].create_index([[ “tiles.position”, Mongo::GEO2D ]]) => “position_2d”
  • 18. Geo2D in 1.8 Data and Indices Querying Running / Scaling Production
  • 19. A caveat: We’re weird. VS http://www.flickr.com/photos/toasty/1540997910/ http://www.flickr.com/photos/vizzzual-dot-com/2175221420/
  • 20. A caveat: We’re weird. VS
  • 21. Problems we don’t have Projection issues Great Circle distance calculation Polar coordinate systems Pirates http://www.flickr.com/photos/jmd41280/4501410061/
  • 22. Querying real location data Search by proximity: $near Uses native units (degrees for [-180, 180]) Use $maxDistance to bound query > db[‘tile’].find(:position => {“$near” => [10,10]}).to_a => [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'), "letter"=>"A", "position"=>[12,9]}] > db[‘tile’].find(:position => {“$near” => [10,10], “$maxDistance” => 1}).to_a =>[]
  • 23. Querying real location data Need distance to center as well? Use $geoNear Also includes fun stats > db.command('geoNear' => 'tiles', 'near' => [1830, 2002], :maxDistance => 10) ) => {"ns"=>"test.tiles", "near"=>"110000000000001100011000110010101010001000001011 1111", "results"=>[{"dis"=>3.999471664428711, "obj"=>{"_id"=>BSON::ObjectId('4dd0b0957a701852bc02bf67') , "position"=>{"x"=>1830, "y"=>2006}, "letter"=>"A"}}], "stats"=>{"time"=>0, "btreelocs"=>3, "nscanned"=>2, "objectsLoaded"=>1, "avgDistance"=>3.999471664428711, "maxDistance"=>3.999471664428711}, "ok"=>1.0}
  • 24. Querying real location data Region queries: $within Example: $box (rectangle) > db[‘tile’].find(:position => {“$within” => {“$box” => [[10,10], [30,30]]}).to_a => [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'), "letter"=>"A", "position"=>[12,9]}] [30,30] [10,10]
  • 25. Querying real location data Alternately: $center (circle) > db[‘tile’].find(:position => {“$within” => {“$center” => [[10,10], 5]}).to_a => [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'), "letter"=>"A", "position"=>[12,9]}] 5 [10,10]
  • 26. Querying real location data 1.9.x bonus feature: $polygon! > db[‘tile’].find(:position => {“$within” => {“$polygon” => [[5,5], [5,15], [15,5]}).to_a => [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'), "letter"=>"A", "position"=>[12,9]}] [5,15] [5,5] [15,5]
  • 27. Querying real location data Spherical equivalents: $nearSphere and $centerSphere Uses radians, not native units position must be in [long, lat] order! > earthRadius = 6378 #km => 6378 > db[‘restaurants’].find(:position => {“$nearSphere” => [-122.03,36.97], “$maxDistance” => 25.0/earthRadius}).to_a => [{"_id"=>BSON::ObjectId('4dd084ca7a70183256000007'), "name"=>"Crow’s Nest", "position"=>[-122.0,36.96]}]
  • 28. MapReduce using Geo2D in MapReduce and group queries doesn’t work (SERVER-1742) Fixed in 1.8.2 (Yay Greg!)
  • 29. Geo2D in 1.8 Data and Indices Querying Running / Scaling Production
  • 30. Again: we’re weird. Big index, but no need for it all to be in memory Large regions of the world where activity => 0 as density => 1 Single box scaling limit determined by # of active sections of the world at a time
  • 31. Our setup Master/Slave, moving to Replica Set Slaves used for backup Map image generation Next stop: geoSharding
  • 32. Sharding Yes, you can shard on a geo-indexed field Not recommended in 1.8 due to query performance (SERVER-1982). Fix coming in 1.9.x. Can’t use $near in queries, only $geoNear and therefore runCommand(). (SERVER-1981)
  • 33. Questions? http://www.flickr.com/photos/wili/3361117222/ @ggoodale grant@massivelyfun.com

Notes de l'éditeur

  1. \n
  2. Don’t have a lot of time - trying to pack in as much useful information as possible.\n\nStill, in case you’re wondering where the title came from: A book from the 19th Century in which a Sphere comes to 2D Flatland in an effort to convince them of the existence of the third dimension (and then scoffs when the Square posits 4th, 5th etc. dimensions). \n\n
  3. I run a game company named Massively Fun. We’re based in Seattle and are relatively new to the scene. \nOur first game is WordSquared. Anyone heard of it? Anyone? Bueller?\n
  4. Massively multiplayer crossword game, played on an infinitely large board.\nPlay surface: 65MM+ tiles, covering an area of 73156 x 36183\nAssuming a ‘standard’ 4.5mm2 tile, the play surface covers an area of:\n * 53,600 square meters \n * 13.25 acres.\n
  5. One grid square = 15x15 (a size that should be familiar from other, smaller word games)\nEach white pixel = 1 tile\n
  6. \n
  7. Wrote the original version in 48 hours as part of Node Knockout.\nApp is pure HTML/Javascript/CSS3.\nBackend was Node talking to MongoDB.\n
  8. MongoHQ was offering free hosted MongoDB instances for NK teams\nNo fixed schema = less time wrangling with the schema while rapid prototyping\nWe track every tile in the game world using a single Geo2D indexed collection.\n
  9. We’ll be looking at 1.8.1 for today. \nSome things that won’t work in 1.6.x\nSome things get better in 1.9.x (use at your own risk!)\nExamples are in Ruby for brevity and clarity\n
  10. Here’s our basic tile document. Not too different from what we store.\nWe’re going to generate a 2d index across the position field.\nOur position data is in an array. Doesn’t matter whether x or y comes first, as long as it’s consistent.\n
  11. You can also store your position data as a sub-object.\nThere’s a gotcha here...\n
  12. Let’s use Ruby 1.8.7 as an example. If you specify the position as a Hash,\nthere’s no guarantee the ordering of keys will be preserved. \nThis is bad news for geo2d and will result in strange query results.\n
  13. Not much you can do in javascript.\nOf course, your ORM may take care of this for you. Test to be sure.\nWe use the array syntax.\n
  14. Creates a basic lat/long geo2d index over the position column. \nRange by default is -180 to 180, with 26 bits’ precision\nIf you’re indexing a larger space (like us!), you’ll want to increase all 3.\nMin/Max can be really, really big. :)\n
  15. We can modify the defaults by passing additional options to the call to create_index.\nNot that in versions less than 1.9, you cannot insert records at the limit of the index (±500 here).\n
  16. Useful if you’re frequently querying on parameters other than location.\ngeo doesn’t have to be first, but probably should be unless you never query purely on location (remember, only one Geo2D index per collection!)\n
  17. Great for storing things that appear at multiple locations (for example, everywhere on the board a word has been played)\n
  18. \n
  19. The world isn’t flat. (Inorite?) Our world is. \nAny guess which world is easier to deal with?\n
  20. Big surprise.\n
  21. \n
  22. $near is a simple proximity search. $maxDistance can be a float and < 1.\n
  23. Remember, ruby 1.9.x or use OrderedHash! Things won’t work otherwise!\n
  24. This is our primary use: fetching $box queries that represent the user’s viewport on the world.\n
  25. You can also do centerpoint/radius queries.\n
  26. \n
  27. Works like $near, but we need to adjust for the fact that it uses radians and not native units.\nNo $boxSphere or $polygonSphere, in case you were wondering.\n
  28. \n
  29. We’ll be looking at 1.8.1 for today. \nSome things that won’t work in 1.6.x\nSome things get better in 1.9.x (use at your own risk!)\nExamples are in Ruby for brevity and clarity\n
  30. The Word2 world is a bit like the universe. All the interesting stuff is happening further and further apart. \n
  31. \n
  32. Geo queries currently get routed to every shard for execution. \nWe don’t do it (yet). Experimenting with it though.\n
  33. No questions! Look at the seal!\n