com presentations: Mongo Moscow workshop.: mongo-moscow12.xml slides/map/finding-food-import-mongo.xml slides/map/finding-food-take2.xml
slides/map/osm-xapi.xml slides/mongodb-write-import-script.xml slides/mongodb/break.xml slides/mongodb/connecting.xml slides/mongodb/cursors.xml
slides/mongodb/insert.xml slides/mongodb/mongo-extension.xml slides/mongodb/one-to-many2.xml slides/mongodb/one-to-many3.xml
slides/mongodb/one-to-many4.xml slides/mong

From: Date: Sun, 10 Jun 2012 14:10:54 +0000
Subject: com presentations: Mongo Moscow workshop.: mongo-moscow12.xml slides/map/finding-food-import-mongo.xml slides/map/finding-food-take2.xml
slides/map/osm-xapi.xml slides/mongodb-write-import-script.xml slides/mongodb/break.xml slides/mongodb/connecting.xml slides/mongodb/cursors.xml
slides/mongodb/insert.xml slides/mongodb/mongo-extension.xml slides/mongodb/one-to-many2.xml slides/mongodb/one-to-many3.xml
slides/mongodb/one-to-many4.xml slides/mong
Groups: php.pres 
Request: Send a blank email to pres+get-1817@lists.php.net to get a copy of this message
Commit: 07ab009b1157dac02a8e68799d895e856b7961b2 Author: Derick Rethans <github@derickrethans.nl> Sun, 10 Jun 2012 18:07:37 +0400 Parents: 19dae276434970616a8f1821060c7546e20c4c0b Branches: master Link: http://git.php.net/?p=presentations.git;a=commitdiff;h=07ab009b1157dac02a8e68799d895e856b7961b2 Log: Mongo Moscow workshop. Changed paths: A mongo-moscow12.xml M slides/map/finding-food-import-mongo.xml M slides/map/finding-food-take2.xml M slides/map/osm-xapi.xml A slides/mongodb-write-import-script.xml A slides/mongodb/break.xml M slides/mongodb/connecting.xml M slides/mongodb/cursors.xml M slides/mongodb/insert.xml M slides/mongodb/mongo-extension.xml M slides/mongodb/one-to-many2.xml M slides/mongodb/one-to-many3.xml M slides/mongodb/one-to-many4.xml A slides/mongodb/osm-to-mongo-data.xml A slides/mongodb/osm-to-mongo-data2.xml A slides/mongodb/osm-to-mongo-data3.xml A slides/mongodb/osm-to-mongo-data4.xml A slides/mongodb/php-connection-string.xml M slides/mongodb/query-advanced-example-array2.xml M slides/mongodb/query.xml M slides/mongodb/query2.xml A slides/mongodb/read-preference.xml A slides/mongodb/replication-setup.xml M slides/mongodb/safe.xml M slides/mongodb/safe2.xml A slides/mongodb/safe3.xml A slides/mongodb/write-import-script-result.xml A slides/mongodb/write-import-script.xml A slides/mongodb/write-import-script2.xml

diff --git a/mongo-moscow12.xml b/mongo-moscow12.xml new file mode 100644 index 0000000..4df7380 --- /dev/null +++ b/mongo-moscow12.xml @@ -0,0 +1,144 @@ +<?xml version="1.0" encoding="utf-8"?> +<presentation css="10gen.css"> +<topic>mongoDB</topic> +<title>MongoDB workshop</title> +<event>DevConf12</event> +<location>Moscow, Russian Federation</location> +<date>Jun 10th, 2012</date> +<speaker>Derick Rethans</speaker> +<email>derick@10gen.com</email> +<twitter>derickr</twitter> +<url>http://derickrethans.nl/talks.html</url> +<joindin></joindin> +<slide>slides/mongodb/title.xml</slide> +<slide>slides/mongodb/me.xml</slide> + +<slide>slides/mongodb/databasefield.xml</slide> +<slide>slides/mongodb/collections-documents.xml</slide> +<slide>slides/mongodb/document.xml</slide> + +<slide>slides/mongodb/mongo-extension.xml</slide> +- install + - driver + - mongodb + +<slide>slides/mongodb/connecting.xml</slide> + +<slide>slides/mongodb/insert.xml</slide> +- single + +<slide>slides/mongodb/types.xml</slide> +- php types +- extra types (int32/64) +- id +<slide>slides/mongodb/id.xml</slide> +<slide>slides/mongodb/safe.xml</slide> +<slide>slides/mongodb/safe2.xml</slide> + +<slide>slides/mongodb/query.xml</slide> +<slide>slides/mongodb/query2.xml</slide> +- findOne +- find + +<slide>slides/mongodb/cursors.xml</slide> +- cursor +<slide>slides/mongodb/limit-skip.xml</slide> +- limit/skip + + +<slide>slides/mongodb/update-modifiers.xml</slide> +<slide>slides/mongodb/update2a.xml</slide> + +<slide>slides/mongodb/indexes2.xml</slide> +<slide>slides/mongodb/indexes3.xml</slide> +<slide>slides/mongodb/indexes4.xml</slide> + +<slide>slides/mongodb/commands.xml</slide> +<slide>slides/mongodb/commands-distinct.xml</slide> + +<!-- SCHEMA DESIGN --> +Common Patterns + +- RDBMS-normalisation. + +<slide>slides/mongodb/normalisation.xml</slide> +<slide>slides/mongodb/blog.xml</slide> +<slide>slides/mongodb/blog2.xml</slide> + +<slide>slides/mongodb/considerations.xml</slide> + +<slide>slides/mongodb/inherit.xml</slide> +<slide>slides/mongodb/inherit-rdbms.xml</slide> +<slide>slides/mongodb/inherit-mongo.xml</slide> +<slide>slides/mongodb/inherit-summary.xml</slide> + +<slide>slides/mongodb/one-to-many1.xml</slide> +<slide>slides/mongodb/one-to-many2.xml</slide> +<slide>slides/mongodb/one-to-many3.xml</slide> +<slide>slides/mongodb/one-to-many4.xml</slide> + +<slide>slides/mongodb/many-to-many1.xml</slide> +<slide>slides/mongodb/many-to-many2.xml</slide> +<slide>slides/mongodb/many-to-many3.xml</slide> +<slide>slides/mongodb/many-to-many4.xml</slide> + +<slide>slides/mongodb/embed-vs-link.xml</slide> + +<!-- INTRODUCING OSM DATA --> +<slide>slides/map/osm-what.xml</slide> +<slide>slides/map/what-is-osm.xml</slide> +<slide>slides/map/osm-data.xml</slide> + +<slide>slides/map/osm-tags1.xml</slide> +<slide>slides/map/osm-tags2.xml</slide> +<slide>slides/map/osm-tags3.xml</slide> +<slide>slides/map/osm-tags4.xml</slide> + +<slide>slides/map/osm-parse-into-db.xml</slide> +<slide>slides/map/osm-xapi.xml</slide> +<slide>slides/mongodb/write-import-script-result.xml</slide> +<slide>slides/mongodb/osm-to-mongo-data.xml</slide> +<slide>slides/mongodb/osm-to-mongo-data2.xml</slide> +<slide>slides/mongodb/osm-to-mongo-data3.xml</slide> +<slide>slides/mongodb/osm-to-mongo-data4.xml</slide> +<slide>slides/map/finding-food-import-mongo.xml</slide> +<slide>slides/mongodb/write-import-script2.xml</slide> + +<slide>slides/mongodb/indexes-geo.xml</slide> +<slide>slides/mongodb/indexes-geo2.xml</slide> + +<!--<slide>slides/map/finding-food-data-mongo.xml</slide>--> +<slide>slides/map/finding-food-take2.xml</slide> + +<slide>slides/mongodb/replication.xml</slide> +<slide>slides/mongodb/replication-setup.xml</slide> +- setup +- slaveOk +- journal +- write concerns +<slide>slides/mongodb/safe3.xml</slide> +<slide>slides/mongodb/read-preference.xml</slide> +<slide>slides/mongodb/php-connection-string.xml</slide> + +<slide>slides/mongodb/sharding.xml</slide> +- mongos +- diagram +- transparent + +<slide>slides/mongodb/resources.xml</slide> + +<!-- PERFORMANCE THINGS --> +<slide>slides/mongodb/mmap.xml</slide> + + +<!-- ADVANCED QUERY STUFF --> +<slide>slides/mongodb/query-advanced-ops.xml</slide> +<slide>slides/mongodb/query-advanced-example-array1.xml</slide> +<slide>slides/mongodb/query-advanced-example-array2.xml</slide> +<slide>slides/mongodb/upsert.xml</slide> +<slide>slides/mongodb/tip-array-key.xml</slide> +<slide>slides/mongodb/tip-paged-result.xml</slide> + +<slide>slides/mongodb/resources.xml</slide> + +</presentation> diff --git a/slides/map/finding-food-import-mongo.xml b/slides/map/finding-food-import-mongo.xml index b534ccc..74ade27 100644 --- a/slides/map/finding-food-import-mongo.xml +++ b/slides/map/finding-food-import-mongo.xml @@ -1,8 +1,6 @@ <slide> -<title>Importing data</title> +<title>Think about the data schema!</title> -<blurb>MongoDB is schema less, so fits the OSM data model quite well, but you still -need to think about how to store the data best:</blurb> <example>$doc = array( 'name' => 'A440', 'tags = [ @@ -10,7 +8,6 @@ need to think about how to store the data best:</blurb> 'oneway' => 'yes' ] ); - $db->poi->ensureIndex( [ 'name' => 1 ] ); $db->poi->ensureIndex( [ 'tags.highway' => 1 ] ); $db->poi->ensureIndex( [ 'tags.oneway' => 1 ] );</example> @@ -22,7 +19,6 @@ $db->poi->ensureIndex( [ 'tags.oneway' => 1 ] );</example> { 'k' => 'oneway', 'v' => 'yes' } ] ); - $db->poi->ensureIndex( [ 'name' => 1 ] ); $db->poi->ensureIndex( [ 'tags.k' => 1, 'tags.v' => 1 ] );</example> </slide> diff --git a/slides/map/finding-food-take2.xml b/slides/map/finding-food-take2.xml index 72ef0a7..568b395 100644 --- a/slides/map/finding-food-take2.xml +++ b/slides/map/finding-food-take2.xml @@ -1,5 +1,5 @@ <slide> -<title>Finding Food</title> +<title>Seeing the data</title> <iframe filename='examples/find-poi-mongo2.php'/> </slide> diff --git a/slides/map/osm-xapi.xml b/slides/map/osm-xapi.xml index 0ef3e8b..f1868b3 100644 --- a/slides/map/osm-xapi.xml +++ b/slides/map/osm-xapi.xml @@ -2,6 +2,8 @@ <title>Fetching OSM data</title> <example><![CDATA[wget http://download.geofabrik.de/osm/europe/great_britain/england.osm.bz2]]></example> +<blurb>(Or pick a smaller country from +%http://download.geofabrik.de/osm/europe/%)</blurb> <example><![CDATA[ <?xml version='1.0' encoding='UTF-8'?> diff --git a/slides/mongodb-write-import-script.xml b/slides/mongodb-write-import-script.xml new file mode 100644 index 0000000..6e23ae6 --- /dev/null +++ b/slides/mongodb-write-import-script.xml @@ -0,0 +1,28 @@ +<slide> +<title>Exercise 1: Connecting and creating indexes</title> + +<list> + <bullet>%wget http://derickrethans.nl/files/dump/mongo-exercise1.php.txt%</bullet> + <bullet>Open it in your favourite editor and edit.</bullet> +</list> +<example><![CDATA[<?php +$file = $argv[1]; + +// 1 ADD: Set variable $m to the connection, and $d to the collection "poi" in +// the database "demo" on localhost +$m = +$d = + +// 2 ADD: Drop the collection +$d-> + +// 3 ADD: Create an index on "name" and a "2d" index on "loc" +$d->ensureIndex( +$d->ensureIndex( + +var_dump($d->getIndexInfo()); +]]></example> +<list> + <bullet>Run the script!</bullet> +</list> +</slide> diff --git a/slides/mongodb/break.xml b/slides/mongodb/break.xml new file mode 100644 index 0000000..bb2fef8 --- /dev/null +++ b/slides/mongodb/break.xml @@ -0,0 +1,3 @@ +<slide> +<title>Break/Demo</title> +</slide> diff --git a/slides/mongodb/connecting.xml b/slides/mongodb/connecting.xml index 2f76073..a01a085 100644 --- a/slides/mongodb/connecting.xml +++ b/slides/mongodb/connecting.xml @@ -1,5 +1,5 @@ <slide> -<title>Connecting to mongoDB</title> +<title>Connecting to MongoDB</title> <blurb>No tables or collections have to do be explicitly created:</blurb> <break /> @@ -17,6 +17,5 @@ $collection = $database->testCollection; <bullet>%new Mongo("mongodb://localhost");%</bullet> <bullet>%new Mongo("mongodb://localhost:29000");%</bullet> <bullet>%new Mongo("mongodb://mongo.example.com");%</bullet> - <bullet>%new Mongo("mongodb://mdb1.example.com,mdb2.example.com",%<br/>%&#160; [ 'replicaSet' => 'testSet' ] );%</bullet> </list> </slide> diff --git a/slides/mongodb/cursors.xml b/slides/mongodb/cursors.xml index f3a565e..13f567d 100644 --- a/slides/mongodb/cursors.xml +++ b/slides/mongodb/cursors.xml @@ -7,13 +7,13 @@ iterator in the form of a %MongoCursor% object.</blurb> <example result="1"><![CDATA[<?php $m = new Mongo(); -$c = $m->demo->articles; +$c = $m->demo->talks; -$cursor = $c->find( [ 'title' => [ '$exists' => true ] ] ); +$cursor = $c->find( [ 'talks.title' => [ '$exists' => true ] ] ); foreach ( $cursor as $r ) { - echo $r['title'], ': ', join( ', ', $r['tags'] ), "\n"; + echo $r['_id'], "\n"; } ?>]]></example> </slide> diff --git a/slides/mongodb/insert.xml b/slides/mongodb/insert.xml index e3a3fb9..9db5f47 100644 --- a/slides/mongodb/insert.xml +++ b/slides/mongodb/insert.xml @@ -6,7 +6,7 @@ { "_id" : "derickr", "name" : "Derick Rethans", - "articles" : [ + "talks" : [ { "title" : "Profiling PHP Applications", "url" : "http://derickrethans.nl/talks/profiling-phptour.pdf", @@ -25,7 +25,7 @@ $document = array( "_id" => "derickr", "name" => "Derick Rethans", - "articles" => array( + "talks" => array( array( "title" => "Profiling PHP Applications", "url" => "http://derickrethans.nl/talks/profiling-phptour.pdf", @@ -38,16 +38,16 @@ $document = array( ); $m = new Mongo(); -var_dump( $m->demo->articles->insert( $document ) ); +var_dump( $m->demo->talks->insert( $document ) ); ?>]]></example> </div> <div effect="fade-in"> -<example result="0"><![CDATA[<?php +<example result="1"><![CDATA[<?php $document = [ "_id" => "derickr", "name" => "Derick Rethans", - "articles" => [ + "talks" => [ [ "title" => "Profiling PHP Applications", "url" => "http://derickrethans.nl/talks/profiling-phptour.pdf", @@ -60,7 +60,7 @@ $document = [ ]; $m = new Mongo(); -var_dump( $m->demo->articles->insert( $document ) ); +var_dump( $m->demo->talks->insert( $document ) ); ?>]]></example> </div> </slide> diff --git a/slides/mongodb/mongo-extension.xml b/slides/mongodb/mongo-extension.xml index f52b59f..eeff9c5 100644 --- a/slides/mongodb/mongo-extension.xml +++ b/slides/mongodb/mongo-extension.xml @@ -1,10 +1,10 @@ <slide> -<title>The mongoDB PHP extension</title> +<title>The MongoDB PHP extension</title> <image filename="10gen.png" align="right"/> <list> - <bullet>Maintained and supported by 10gen (Kristina and me)</bullet> + <bullet>Maintained and supported by 10gen</bullet> <bullet>Can be installed with pecl: %pecl install mongo%</bullet> <bullet>Add %extension=mongo.so% to %php.ini%</bullet> </list> diff --git a/slides/mongodb/one-to-many2.xml b/slides/mongodb/one-to-many2.xml index 6b4d516..d6b0dff 100644 --- a/slides/mongodb/one-to-many2.xml +++ b/slides/mongodb/one-to-many2.xml @@ -3,19 +3,22 @@ <blurb>Embedded Array / Array Keys</blurb> <list> - <bullet>slice operator to return subset of array</bullet> - <bullet>some queries harder e.g find latest comments across all documents</bullet> + <bullet>some queries harder: e.g find all areas speaking German</bullet> </list> -<example><![CDATA[blogs: { - author: "Hergé", - date: "Tue Mar 28 2012 12:41:29 GMT", - comments: [ - { - author: "Kyle", - date: "Tue Mar 28 2012 12:41:54 GMT", - text: "great book" - } +<example><![CDATA[{ + name: "Germany", + language: "German", + areas: [ + { name: "Bavaria", language: "Swabian" } + ] +}, +{ + name: "Switzerland", + areas: [ + { name: "Deutsche Schweiz", language: "German" }, + { name: "Romandy", language: "French" }, + { name: "Svizzera italiana", language: "Italian" }, ] }]]></example> </slide> diff --git a/slides/mongodb/one-to-many3.xml b/slides/mongodb/one-to-many3.xml index 62b71a5..1ea0f70 100644 --- a/slides/mongodb/one-to-many3.xml +++ b/slides/mongodb/one-to-many3.xml @@ -9,15 +9,23 @@ </list> <example><![CDATA[blogs: { - author: "Hergé", - date: "Tue Mar 28 2012 12:41:29 GMT", + author: "sambeau", + date: "84 days ago", comments: [ { - author: "Kyle", - date: "Tue Mar 28 2012 12:41:54 GMT", - text: "great book" + author: "ElliotH", + date: "84 days ago", + text: "It’s also missing the necessary credit to OpenStreetMap’s + contributors; we look forward to working with Apple to get that on + there.", replies: [ - { author: "Derick", ... } + { + author: "sambeau", + date: "83 days ago", + text: "I do think that this is a very classy move by the OSM people, + no ranting blog post or 'Apple stole our stuff', welcoming people + presents a much better image of the project." + } ] } ] diff --git a/slides/mongodb/one-to-many4.xml b/slides/mongodb/one-to-many4.xml index eaedd2c..bd0700b 100644 --- a/slides/mongodb/one-to-many4.xml +++ b/slides/mongodb/one-to-many4.xml @@ -1,28 +1,23 @@ <slide> <title>One to Many (1:n)</title> -<blurb>Normalised (with two collections)</blurb> +<blurb>Split out into multiple documents</blurb> <list> <bullet>most flexble</bullet> <bullet>more queries</bullet> </list> -<example><![CDATA[blogs: { - author: "Hergé", - date: "Tue Mar 28 2012 12:41:29 GMT", - comments: [ - { comment: "1" } - ] -} +<example><![CDATA[ +{ name: "Germany", language: "German", }, +{ name: "Bavaria", language: "Swabian", part_of: "Germany" }, -comments: { - _id: "1", - author: "Kyle", - date: "Tue Mar 28 2012 12:41:54 GMT", -}, +// or: { - _id: "2", - author: "Derick", - in_reply_to: "1", -}]]></example> + name: "Switzerland", + parts: [ 'Deutsche Schweiz', 'Romandy', 'Svizzera italiana' ] +}, +{ name: "Deutsche Schweiz", language: "German" }, +{ name: "Romandy", language: "French" }, +{ name: "Svizzera italiana", language: "Italian" }, +]]></example> </slide> diff --git a/slides/mongodb/osm-to-mongo-data.xml b/slides/mongodb/osm-to-mongo-data.xml new file mode 100644 index 0000000..e38faa7 --- /dev/null +++ b/slides/mongodb/osm-to-mongo-data.xml @@ -0,0 +1,41 @@ +<slide> +<title>Importing OSM into MongoDB</title> + +<blurb>From %http://www.openstreetmap.org/browse/way/4442243%:</blurb> +<example> +Way: Brondesbury Road (4442243) +Tags: highway = secondary + name = Brondesbury Road + ref = B451 + source:ref = OS OpenData StreetView +</example> + +<div effect="fade-in-out"> +<blurb>Simplest way:</blurb> +<example> +{ + '_id': 'w4442243', + 'highway': 'secondary', + 'name' : 'Brondesbury Road', + 'ref' : 'B451', + 'source_ref' : 'OS OpenData StreetView', +} +</example> +</div> + +<div effect="fade-in"> +<blurb>or a little more organised:</blurb> +<example> +{ + '_id': 'w4442243', + 'name' : 'Brondesbury Road', + 'tags': { + 'highway': 'secondary', + 'ref' : 'B451', + 'source_ref' : 'OS OpenData StreetView', + } +} +</example> +</div> + +</slide> diff --git a/slides/mongodb/osm-to-mongo-data2.xml b/slides/mongodb/osm-to-mongo-data2.xml new file mode 100644 index 0000000..5c60eb3 --- /dev/null +++ b/slides/mongodb/osm-to-mongo-data2.xml @@ -0,0 +1,19 @@ +<slide> +<title>Importing OSM into MongoDB (2)</title> + +<example> +{ + '_id': 'w4442243', + 'name' : 'Brondesbury Road', + 'tags': { + 'highway': 'secondary', + 'ref' : 'B451', + 'source_ref' : 'OS OpenData StreetView', + } +} +</example> +<blurb>Querying all secondary roads:</blurb> +<example>db.poi.find( { 'tags.highway' : 'secondary' } );</example> +<blurb>Find all roads:</blurb> +<example>db.poi.find( { 'tags.highway' : { $exists : true } );</example> +</slide> diff --git a/slides/mongodb/osm-to-mongo-data3.xml b/slides/mongodb/osm-to-mongo-data3.xml new file mode 100644 index 0000000..ab8fe0b --- /dev/null +++ b/slides/mongodb/osm-to-mongo-data3.xml @@ -0,0 +1,34 @@ +<slide> +<title>Importing OSM into MongoDB (3)</title> + +<div effect="fade-out"> +<blurb>Find all pubs:</blurb> +<example>db.poi.find( { 'tags.amenity' : 'pub' } );</example> + +<blurb>Let's look at the "explain" output:</blurb> +<example inline="1">db.poi.find( { 'tags.amenity' : 'pub' } ).explain(); +{ + "cursor" : "BasicCursor", + "isMultiKey" : false, +*|e70000| "n" : 3895,|* +*|e70000| "nscannedObjects" : 2347913,|* + "nscanned" : 2347913, + ... +</example> +</div> + +<div effect="fade-in"> +<blurb>We need an index for that:</blurb> +<example>db.poi.ensureIndex( { "tags.amenity" : 1 } );</example> +<blurb>And the new "explain" output:</blurb> +<example inline="1">db.poi.find( { 'tags.amenity' : 'pub' } ).explain(); +{ + "cursor" : "BtreeCursor tags.amenity_1", + "isMultiKey" : false, + "n" : 3895, +*|00e700| "nscannedObjects" : 3895,|* + "nscanned" : 3895, + ... +</example> +</div> +</slide> diff --git a/slides/mongodb/osm-to-mongo-data4.xml b/slides/mongodb/osm-to-mongo-data4.xml new file mode 100644 index 0000000..e8056f2 --- /dev/null +++ b/slides/mongodb/osm-to-mongo-data4.xml @@ -0,0 +1,29 @@ +<slide> +<title>Importing OSM into MongoDB (4)</title> + +<example> +{ + '_id': 'w4442243', + 'name' : 'Brondesbury Road', + 'tags': { + 'highway': 'secondary', + 'ref' : 'B451', + 'source_ref' : 'OS OpenData StreetView', + } +} +</example> +<example>db.poi.ensureIndex( { "tags.amenity" : 1 } );</example> +<div effect="fade-in"> +<example>db.poi.ensureIndex( { "tags.highway" : 1 } );</example> +</div> +<div effect="fade-in"> +<example>db.poi.ensureIndex( { "tags.ref" : 1 } );</example> +</div> +<div effect="fade-in"> +<example>db.poi.ensureIndex( { "tags.???" : 1 } );</example> +<list> + <bullet>Lots of indexes make things slow</bullet> + <bullet>Limited to 64 indexes</bullet> +</list> +</div> +</slide> diff --git a/slides/mongodb/php-connection-string.xml b/slides/mongodb/php-connection-string.xml new file mode 100644 index 0000000..fdd06e8 --- /dev/null +++ b/slides/mongodb/php-connection-string.xml @@ -0,0 +1,20 @@ +<slide> +<title>Connection String</title> + +<example result="0"><![CDATA[<?php +$options = array( 'replicaSet' => 'seta' ); + +$m = new Mongo( 'mongodb://localhost:13000', $options ); + +$m = new Mongo( 'mongodb://localhost:13000,localhost:13001', $options ); + +$m = new Mongo( 'mongodb://user:password@localhost:13000,localhost:13001/demo', $options ); +?>]]></example> + +<list> + <bullet>Add more than one host for seeding</bullet> + <bullet>Don't add the arbiters to the connection string</bullet> + <bullet>Don't use %->authenticate()%</bullet> +</list> + +</slide> diff --git a/slides/mongodb/query-advanced-example-array2.xml b/slides/mongodb/query-advanced-example-array2.xml index 45ad33d..90545be 100644 --- a/slides/mongodb/query-advanced-example-array2.xml +++ b/slides/mongodb/query-advanced-example-array2.xml @@ -4,12 +4,10 @@ <example result="1"><![CDATA[<?php $m = new Mongo; $c = $m->demo->circus; -$r = $c->find( - array ( - 'elephpants' => - array( '$elemMatch' => array ( 'colour' => 'blue', 'year' => array( '$gt' => 1900 ) ) - ) -) ); +$r = $c->find( [ + 'elephpants' => + [ '$elemMatch' => [ 'colour' => 'blue', 'year' => [ '$gt' => 1900 ] ] ] +] ); foreach( $r as $c ) { echo $c['name'], ': '; foreach( $c['elephpants'] as $elephpant ) { diff --git a/slides/mongodb/query.xml b/slides/mongodb/query.xml index 96fad3e..116a5a0 100644 --- a/slides/mongodb/query.xml +++ b/slides/mongodb/query.xml @@ -4,15 +4,15 @@ <example><![CDATA[<?php $m = new Mongo(); // First "found" item: -$record = $m->demo->articles->findOne(); +$record = $m->demo->talks->findOne(); // Search on the _id field being 'derickr' -$record = $m->demo->articles->findOne( array( '_id' => 'derickr' ) ); +$record = $m->demo->talks->findOne( array( '_id' => 'derickr' ) ); -// Search on the array element key 'title' in the 'articles' field -$record = $m->demo->articles->findOne( +// Search on the array element key 'title' in the 'talks' field +$record = $m->demo->talks->findOne( array( - 'articles.title' => 'Xdebug' + 'talks.title' => 'Xdebug' ) ); ?>]]></example> diff --git a/slides/mongodb/query2.xml b/slides/mongodb/query2.xml index deca42d..afffa95 100644 --- a/slides/mongodb/query2.xml +++ b/slides/mongodb/query2.xml @@ -5,9 +5,9 @@ $m = new Mongo(); // Find with 'projection' -$record = $m->demo->articles->findOne( - array( 'articles.title' => 'Xdebug' ), - array( 'name' => true, 'articles.url' => true ) +$record = $m->demo->talks->findOne( + array( 'talks.title' => 'Xdebug' ), + array( 'name' => true, 'talks.url' => true ) ); ?>]]></example> </slide> diff --git a/slides/mongodb/read-preference.xml b/slides/mongodb/read-preference.xml new file mode 100644 index 0000000..1db5d46 --- /dev/null +++ b/slides/mongodb/read-preference.xml @@ -0,0 +1,16 @@ +<slide> +<title>Read preferences</title> +<subtitle>(in development)</subtitle> + +<example result="0"><![CDATA[<?php +$m = new Mongo( 'mongodb://localhost:13000' ); +$c = $m->demo->poi; +$c->setReadPreference( Mongo::READ_NEAREST, array( 'dc' => 'europe1' ) ); + +$c->find( ... ); + +// or +$c->find( ... )->setReadPreference( Mongo::SECONDARY ); +?>]]></example> + +</slide> diff --git a/slides/mongodb/replication-setup.xml b/slides/mongodb/replication-setup.xml new file mode 100644 index 0000000..3f1dd00 --- /dev/null +++ b/slides/mongodb/replication-setup.xml @@ -0,0 +1,39 @@ +<slide> +<title>Setting up replication</title> + +<blurb>Starting the daemons (2 data, 1 arbiter):</blurb> + +<example> +mongod -f /etc/mongodb.conf --dbpath=~/repl-slave0 --port 13000 --replSet seta +mongod -f /etc/mongodb.conf --dbpath=~/repl-slave1 --port 13001 --replSet seta +mongod -f /etc/mongodb.conf --dbpath=~/repl-arb --port 13002 --replSet seta --rest +</example> + +<div effect="fade-out"> +<blurb>Sample %/etc/mongodb.conf%</blurb> +<example> +logpath=/var/log/mongodb/mongodb.log +logappend=true +smallfiles=true +replSet=seta +</example> +</div> +<div effect="fade-in"> +<blurb>Configure the set:</blurb> +<example> +mongo localhost:13000 + +> db.adminCommand( { + replSetInitiate: { + _id: 'seta', + members: [ + { _id: 0, host: 'localhost:13000' }, + { _id: 1, host: 'localhost:13001' }, + ] + } +} ); + +PRIMARY> rs.addArb('localhost:13002'); +</example> +</div> +</slide> diff --git a/slides/mongodb/safe.xml b/slides/mongodb/safe.xml index 64e7fe5..3fd7e96 100644 --- a/slides/mongodb/safe.xml +++ b/slides/mongodb/safe.xml @@ -6,7 +6,7 @@ <div effect="fade-out"> <example><![CDATA[<?php $m = new Mongo; -$c = $m->demo->articles; +$c = $m->demo->talks; $c->insert( array( '_id' => 'derickr' ) ); $c->insert( array( '_id' => 'derickr' ) ); @@ -17,7 +17,7 @@ $c->insert( array( '_id' => 'derickr' ) ); <blurb>"Safe" inserts:</blurb> <example result="1"><![CDATA[<?php $m = new Mongo; -$c = $m->demo->articles; +$c = $m->demo->talks; try { $c->insert( diff --git a/slides/mongodb/safe2.xml b/slides/mongodb/safe2.xml index daf6d3c..99ebf37 100644 --- a/slides/mongodb/safe2.xml +++ b/slides/mongodb/safe2.xml @@ -11,7 +11,7 @@ <example result="1"><![CDATA[<?php $m = new Mongo(); -$c = $m->demo->articles; +$c = $m->demo->talks; $c->insert( array( '_id' => 'mongodb' ), // document diff --git a/slides/mongodb/safe3.xml b/slides/mongodb/safe3.xml new file mode 100644 index 0000000..a4f62df --- /dev/null +++ b/slides/mongodb/safe3.xml @@ -0,0 +1,18 @@ +<slide> +<title>Write concerns</title> + +<blurb>Safeness levels:</blurb> +<list> + <bullet>Confirm replication: %array( 'safe' => true, 'w' => 2 );%</bullet> +</list> + +<example result="1"><![CDATA[<?php +$m = new Mongo( 'mongodb://localhost:13000' ); +$c = $m->demo->talks; + +$c->insert( + array( '_id' => 'mongodb' ), // document + array( 'safe' => true, 'w' => 2 ) // options +); +?>]]></example> +</slide> diff --git a/slides/mongodb/write-import-script-result.xml b/slides/mongodb/write-import-script-result.xml new file mode 100644 index 0000000..3e45f76 --- /dev/null +++ b/slides/mongodb/write-import-script-result.xml @@ -0,0 +1,28 @@ +<slide> +<title>Exercise 1: Connecting and creating indexes</title> + +<list> + <bullet>%wget http://derickrethans.nl/files/dump/mongo-exercise1.php.txt%</bullet> + <bullet>Open it in your favourite editor and edit.</bullet> +</list> +<example><![CDATA[<?php +$file = $argv[1]; + +// 1 ADD: Set variable $m to the connection, and $d to the collection "poi" in +// the database "demo" on localhost +$m = new Mongo(); +$d = $m->demo->poi; + +// 2 ADD: Drop the collection +$d->drop(); + +// 3 ADD: Create an index on "name" and a "2d" index on "loc" +$d->ensureIndex( array( 'loc' => '2d' ) ); +$d->ensureIndex( array( 'name' => 1 ) ); + +var_dump($d->getIndexInfo()); +]]></example> +<list> + <bullet>Run the script!</bullet> +</list> +</slide> diff --git a/slides/mongodb/write-import-script.xml b/slides/mongodb/write-import-script.xml new file mode 100644 index 0000000..6e23ae6 --- /dev/null +++ b/slides/mongodb/write-import-script.xml @@ -0,0 +1,28 @@ +<slide> +<title>Exercise 1: Connecting and creating indexes</title> + +<list> + <bullet>%wget http://derickrethans.nl/files/dump/mongo-exercise1.php.txt%</bullet> + <bullet>Open it in your favourite editor and edit.</bullet> +</list> +<example><![CDATA[<?php +$file = $argv[1]; + +// 1 ADD: Set variable $m to the connection, and $d to the collection "poi" in +// the database "demo" on localhost +$m = +$d = + +// 2 ADD: Drop the collection +$d-> + +// 3 ADD: Create an index on "name" and a "2d" index on "loc" +$d->ensureIndex( +$d->ensureIndex( + +var_dump($d->getIndexInfo()); +]]></example> +<list> + <bullet>Run the script!</bullet> +</list> +</slide> diff --git a/slides/mongodb/write-import-script2.xml b/slides/mongodb/write-import-script2.xml new file mode 100644 index 0000000..4e97689 --- /dev/null +++ b/slides/mongodb/write-import-script2.xml @@ -0,0 +1,18 @@ +<slide> +<title>Exercise 2: Add index for tags</title> + +<list> + <bullet>%wget http://derickrethans.nl/files/dump/mongo-exercise2.php.txt%</bullet> + <bullet>Open it in your favourite editor and edit.</bullet> +</list> +<example><![CDATA[<?php +// 1 ADD: Indexes for the tags +$d->ensureIndex( array( 'tags_indexed' => 1 ) ); +$d->ensureIndex( array( 'tags_indexed.k' => 1, 'tags_indexed.v' => 1 ) ); + +var_dump($d->getIndexInfo()); +]]></example> +<list> + <bullet>Run the script!</bullet> +</list> +</slide>
« previous php.pres (#1817) next »