SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
1
PHP
♥
MongoDB
Saturday 18 August 2012
• Hannes / @bjori
• Icelandic
» Oslo, Norway
» London, England
• PHP Driver Engineer
» @10gen
• PHP Contributor
About me
2
Saturday 18 August 2012
Why I started with MongoDB
• Like creating (code, not graphics)
• Productivity
• Don’t want to waste my time
3
Saturday 18 August 2012
The “old” way
4
Saturday 18 August 2012
The “new” way
5
Saturday 18 August 2012
Terminology
RDBMS Mongo
Table, View ➜ Collection
Row ➜ JSON Document
Index ➜ Index
Join ➜ Embedded Document
Partition ➜ Shard
Partition Key ➜ Shard Key
6
Saturday 18 August 2012
PostObject Object(
[title] => MongoDB
[contributors] => Array(
[0] => PersonObject Object(
[name] => Eliot Horowitz
[email] => eh@10gen.com
)
[1] => PersonObject Object(
[name] => Dwight Merriman
[email] => dm@10gen.com
)
)
[model] => ModelObject Object(
[relational] =>
[awesome] => 1
)
)
Tables to Documents
{
title: ‘MongoDB’,
contributors: [
{ name: ‘Eliot Horowitz’,
email: ‘eh@10gen.com’ },
{ name: ‘Dwight Merriman’,
email: ‘dm@10gen.com’ }
],
model: {
relational: false,
awesome: true
}
}
Documents to
Arrays
Documents to Objects
7
Saturday 18 August 2012
PostObject Object(
[title] => MongoDB
[contributors] => Array(
[0] => PersonObject Object(
[name] => Eliot Horowitz
[email] => eh@10gen.com
)
[1] => PersonObject Object(
[name] => Dwight Merriman
[email] => dm@10gen.com
)
)
[model] => ModelObject Object(
[relational] =>
[awesome] => 1
)
)
array(
"title" => 'MongoDB',
"contributors" => array(
array(
'name' => 'Eliot Horowitz',
'email' => 'eh@10gen.com'
),
array(
'name' => 'Dwight Merriman',
'email' => 'dm@10gen.com'
)
),
"model" => array(
'relational' => false,
'awesome' => true
Tables to Documents
{
title: ‘MongoDB’,
contributors: [
{ name: ‘Eliot Horowitz’,
email: ‘eh@10gen.com’ },
{ name: ‘Dwight Merriman’,
email: ‘dm@10gen.com’ }
],
model: {
relational: false,
awesome: true
}
}
Documents to
Arrays
Documents to Objects
7
Saturday 18 August 2012
PostObject Object(
[title] => MongoDB
[contributors] => Array(
[0] => PersonObject Object(
[name] => Eliot Horowitz
[email] => eh@10gen.com
)
[1] => PersonObject Object(
[name] => Dwight Merriman
[email] => dm@10gen.com
)
)
[model] => ModelObject Object(
[relational] =>
[awesome] => 1
)
)
array(
"title" => 'MongoDB',
"contributors" => array(
array(
'name' => 'Eliot Horowitz',
'email' => 'eh@10gen.com'
),
array(
'name' => 'Dwight Merriman',
'email' => 'dm@10gen.com'
)
),
"model" => array(
'relational' => false,
'awesome' => true
Tables to Documents
{
title: ‘MongoDB’,
contributors: [
{ name: ‘Eliot Horowitz’,
email: ‘eh@10gen.com’ },
{ name: ‘Dwight Merriman’,
email: ‘dm@10gen.com’ }
],
model: {
relational: false,
awesome: true
}
}
Documents to
Arrays
Documents to Objects
7
Saturday 18 August 2012
First things first
8
Saturday 18 August 2012
PHP Driver
No dependencies
https://github.com/mongodb/mongo-php-driver/downloads
Maintained and supported by 10gen
$ pecl install mongo
extension = mongo.so
9
Saturday 18 August 2012
Connecting to the DB
If the DB or collection doesn’t exist, mongoDB
will create it on connect.
$connection = new Mongo();
$db = $connection->selectDB('blog');
$posts = $db->post;
10
mongo.default_host = localhost
mongo.default_port = 27017
Saturday 18 August 2012
Connecting to ReplicaSet
If one of the servers is down, we’ll connect to
the next one
Auto-discovery of the RS topology
$connection = new Mongo('server1,server2',
array(‘replicaSet’ => ‘myReplicaSet’));
$db = $connection->selectDB('blog');
$posts = $db->post;
11
Saturday 18 August 2012
Documents
Blog Post Document
$p1 = array("author" => "roger",
"date" => new MongoDate(),
"text" => "about mongoDB...",
"tags" => array('tech', 'databases'),
);
$posts->insert($p1);
12
Saturday 18 August 2012
Silly Billy
No error check!
try {
$posts->insert($p1, array("safe" => true));
catch( Exception $e ) {
echo $e->getMessage(); /* ... */
}
13
Saturday 18 August 2012
Querying
Note: _id is unique, but can be
anything you’d like
> db.posts.findOne()
> { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "roger",
date : "Sat Jul 24 2010 19:47:11",
text : "About MongoDB...",
tags : [ "tech", "databases" ] }
print_r($posts->findOne());
print_r($p1);
Array(
[_id] => MongoId Object(
[$id] => 4e9796764a18173a17000000
)
[author] => roger
[date] => MongoDate Object(
[sec] => 1318557302
[usec] => 581000
)
[text] => about mongoDB...
[tags] => Array(
[0] => tech
[1] => databases
)
)
14
Saturday 18 August 2012
Querying
Note: _id is unique, but can be
anything you’d like
> db.posts.findOne()
> { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "roger",
date : "Sat Jul 24 2010 19:47:11",
text : "About MongoDB...",
tags : [ "tech", "databases" ] }
print_r($posts->findOne());
print_r($p1);
Array(
[_id] => MongoId Object(
[$id] => 4e9796764a18173a17000000
)
[author] => roger
[date] => MongoDate Object(
[sec] => 1318557302
[usec] => 581000
)
[text] => about mongoDB...
[tags] => Array(
[0] => tech
[1] => databases
)
)
14
Saturday 18 August 2012
MongoId
An autogenerated primary key
$x = array("foo" => 1, "bar" => 2);
$db->baz->insert($x);
var_dump($x['_id']);
Result:
object(MongoId)#4 (1) {
["$id"]=> string(24) "4e9cc76a4a1817fd21000000"
}
15
Saturday 18 August 2012
MongoId
An autogenerated primary key
object(MongoId)#4 (1) {
["$id"]=> string(24) "4e9cc76a4a1817fd21000000"
}
12bytes
4e9cc76a4a1817fd21000000
|------||----||--||----|
ts machine pid inc
16
Saturday 18 August 2012
Update Operations
$set, $unset, $inc, $push, $pushAll,
$pull, $pullAll, $bit
$change = array('$push' =>
array('comments' =>
array(
'author' => 'Fred',
'created' => new MongoDate(),
'comment' => 'Best post ever!.'
)
)
);
$posts->update(array("_id" => $id), $change);
17
Saturday 18 August 2012
Array(
[_id] => MongoId Object (
[$id] => 4e9796764a18173a17000000
)
[author] => roger
[comments] => Array(
[0] => Array(
[author] => Fred
[created] => MongoDate Object(
[sec] => 1318899392
[usec] => 176000
)
[comment] => Dumb post.
)
)
[date] => MongoDate Object(
[sec] => 1318557302
[usec] => 581000
)
[tags] => Array(
[0] => tech
[1] => databases
)
[text] => about mongoDB...
)
Nested Documents
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "roger",
date : ISODate("2012-08-13T11:11:00.294Z"),
text : "About MongoDB...",
tags : [ "tech", "databases" ],
comments : [
	 {
	 		 author : "Fred",
	 		 created : ISODate("2012-08-13T11:41:00.634Z"),
	 		 comment : "Best Post Ever!"
	 	}
]
}
18
Saturday 18 August 2012
Array(
[_id] => MongoId Object (
[$id] => 4e9796764a18173a17000000
)
[author] => roger
[comments] => Array(
[0] => Array(
[author] => Fred
[created] => MongoDate Object(
[sec] => 1318899392
[usec] => 176000
)
[comment] => Dumb post.
)
)
[date] => MongoDate Object(
[sec] => 1318557302
[usec] => 581000
)
[tags] => Array(
[0] => tech
[1] => databases
)
[text] => about mongoDB...
)
Nested Documents
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "roger",
date : ISODate("2012-08-13T11:11:00.294Z"),
text : "About MongoDB...",
tags : [ "tech", "databases" ],
comments : [
	 {
	 		 author : "Fred",
	 		 created : ISODate("2012-08-13T11:41:00.634Z"),
	 		 comment : "Best Post Ever!"
	 	}
]
}
18
Saturday 18 August 2012
Querying
$posts = $blog->find(array(
"author" => "Roger"));
$commentsByFred = $blog->find(array(
"comments.author" => "Fred"));
$commentsByFred = $blog->find(array(
"comments.author" =>
new MongoRegex("/fred/i")));
19
Saturday 18 August 2012
It’s all about the $
$ instead of >, <, =, etc.
$gt, $gte, $lt, $lte, $eq, $neq, $exists,
$set, $mod, $where, $in, $nin, $inc
$push, $pull, $pop, $pushAll, $popAll
$c->find(array("x" => array('$gt' => 4)));
$c->find(array("x" => array(“$gt” => 4)));
20
Saturday 18 August 2012
It’s all about the $
$ instead of >, <, =, etc.
$gt, $gte, $lt, $lte, $eq, $neq, $exists,
$set, $mod, $where, $in, $nin, $inc
$push, $pull, $pop, $pushAll, $popAll
$c->find(array("x" => array('$gt' => 4)));
$c->find(array("x" => array(“$gt” => 4)));
20
Saturday 18 August 2012
Adjust the behavior
in php.ini or using ini_set()
php.ini
---------------
mongo.cmd = ":"
$c->find(array("x" => array(':gt' => 4)));
or
ini_set("mongo.cmd", ".");
$c->find(array("x" => array('.gt' => 4)));
21
Saturday 18 August 2012
Indexing
$people->ensureIndex(array("age" => 1));
$people->ensureIndex(array(
"name" => -1,
"ts" => -1,
"comments.author" => 1
));
22
Saturday 18 August 2012
More Indexing
// Index nested documents
$posts->ensureIndex( array("comments.author" => 1) )
$posts->find( array("comments.author" => "Fred") )
// Index on tags (multi-key index)
$posts->ensureIndex( array("tags" => 1 ) )
$posts->find( array("tags" => "tech") )
// geospatial index
$posts->ensureIndex( array("author.location" => "2d") )
$posts->find( array("author.location" =>
array( '$near' => array(22,42)));
23
Saturday 18 August 2012
Cursors
$cursor = $c->find(array("foo" => "bar"))
foreach ($cursor as $id => $value) {
echo "$id: ";
var_dump( $value );
}
$a = iterator_to_array($cursor);
24
Saturday 18 August 2012
Paging
$page_num = 3;
$results_per_page = 10;
$cursor = $results->find()
->sort(array("ts" => -1))
->skip($page_num * $results_per_page)
->limit($results_per_page);
25
Saturday 18 August 2012
Rich Documents
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
line_items : [ { sku: ‘tt-123’,
name: ‘Coltrane: Impressions’ },
{ sku: ‘tt-457’,
name: ‘Davis: Kind of Blue’ } ],
address : { name: ‘Banker’,
street: ‘111 Main’,
zip: 10010 },
payment: { cc: 4567,
exp: Date(2011, 7, 7) },
subtotal: 2355
}
26
Saturday 18 August 2012
Libraries & Frameworks
• Doctrine MongoDB ODM
• Zend Framework
• Lithium
• Symfony2
• Kohana
• Drupal
• Yii
27
Saturday 18 August 2012
Doctrine MongoDB ODM
• Provides full validation
• Seamless integration with Doctrine ORM
– Useful for hybrid solutions
• Follows same persistence model as Doctrine
• Uses a Document Manager & annotations
• Supports embedded and referenced objects
• Can use mongo’s query interface
• Supports in place updates
https://github.com/doctrine/mongodb-odm
28
Saturday 18 August 2012
Grid FS
Saturday 18 August 2012
Storing Files
Under 16mb
Saturday 18 August 2012
Storing Big Files
>16mb stored in 16mb chunks
Saturday 18 August 2012
Storing Big Files
Works with replicated
and sharded systems
Saturday 18 August 2012
A better network FS
• GridFS files are seamlessly sharded & replicated.
• No OS constraints...
• No file size limits
• No naming constraints
• No folder limits
• Standard across different OSs
• MongoDB automatically generate the MD5 hash
of the file
Saturday 18 August 2012
Storing Files
$grid = $db->getGridFS();
// The file's location in the File System
$path = "/tmp/";
$filename = "movie.mp4";
// Note metadata field & filename field
$storedfile = $grid->storeFile($path . $filename, array
("metadata" => array("filename" => $filename), "filename" =>
$filename));
// Return newly stored file's Document ID
echo $storedfile;
Saturday 18 August 2012
Storing Data
$grid = $db->getGridFS();
$filename = "zelda.jpg";
$storedfile = $grid->storeBytes("ÿØÿÉ
€ ^@^PJFIF^@^A^A^A,^G^E^E^G,...",
array("metadata" => array("filename" =>
$filename),
"filename" => $filename));
Saturday 18 August 2012
Streaming Data
$gridFS = $db->getGridFS();
// Find image to stream
$image = $gridFS->findOne("zelda.jpg");
// Stream image to browser
header('Content-type: image/jpeg');
echo $image->getBytes();
Saturday 18 August 2012
PS: We’re hiring!! Contact us at
jobs@10gen.com
Questions?
download at
mongodb.org
37
Saturday 18 August 2012

Weitere ähnliche Inhalte

Was ist angesagt?

Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodJeremy Kendall
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Groupkchodorow
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolatorMichael Limansky
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuerymanugoel2003
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programmingCasear Chu
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptLaurence Svekis ✔
 
Dealing with Legacy PHP Applications
Dealing with Legacy PHP ApplicationsDealing with Legacy PHP Applications
Dealing with Legacy PHP ApplicationsClinton Dreisbach
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)Night Sailer
 
dojo.data, stores and widgets
dojo.data, stores and widgetsdojo.data, stores and widgets
dojo.data, stores and widgetsklipstein
 
Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2RORLAB
 

Was ist angesagt? (18)

Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the Good
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programming
 
Mongo db for C# Developers
Mongo db for C# DevelopersMongo db for C# Developers
Mongo db for C# Developers
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
Mongo db for c# developers
Mongo db for c# developersMongo db for c# developers
Mongo db for c# developers
 
PhoneGap: Local Storage
PhoneGap: Local StoragePhoneGap: Local Storage
PhoneGap: Local Storage
 
jQuery
jQueryjQuery
jQuery
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
 
Dealing with Legacy PHP Applications
Dealing with Legacy PHP ApplicationsDealing with Legacy PHP Applications
Dealing with Legacy PHP Applications
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
dojo.data, stores and widgets
dojo.data, stores and widgetsdojo.data, stores and widgets
dojo.data, stores and widgets
 
Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2
 
Php
PhpPhp
Php
 

Andere mochten auch

Peeling back your Network Layers with Security Onion
Peeling back your Network Layers with Security OnionPeeling back your Network Layers with Security Onion
Peeling back your Network Layers with Security OnionMark Hillick
 
CTF: Bringing back more than sexy!
CTF: Bringing back more than sexy!CTF: Bringing back more than sexy!
CTF: Bringing back more than sexy!Mark Hillick
 
Scareware Traversing the World via Ireland
Scareware Traversing the World via IrelandScareware Traversing the World via Ireland
Scareware Traversing the World via IrelandMark Hillick
 
MongoDB - Who, What & Where!
MongoDB - Who, What & Where!MongoDB - Who, What & Where!
MongoDB - Who, What & Where!Mark Hillick
 
Siber Güvenlik Eğitiminde Uluslararası CTF Yarışmaları
Siber Güvenlik Eğitiminde Uluslararası CTF YarışmalarıSiber Güvenlik Eğitiminde Uluslararası CTF Yarışmaları
Siber Güvenlik Eğitiminde Uluslararası CTF YarışmalarıDr. Emin İslam Tatlı
 
BGA CTF Ethical Hacking Yarışması Çözümleri
BGA CTF Ethical Hacking Yarışması ÇözümleriBGA CTF Ethical Hacking Yarışması Çözümleri
BGA CTF Ethical Hacking Yarışması ÇözümleriBGA Cyber Security
 

Andere mochten auch (6)

Peeling back your Network Layers with Security Onion
Peeling back your Network Layers with Security OnionPeeling back your Network Layers with Security Onion
Peeling back your Network Layers with Security Onion
 
CTF: Bringing back more than sexy!
CTF: Bringing back more than sexy!CTF: Bringing back more than sexy!
CTF: Bringing back more than sexy!
 
Scareware Traversing the World via Ireland
Scareware Traversing the World via IrelandScareware Traversing the World via Ireland
Scareware Traversing the World via Ireland
 
MongoDB - Who, What & Where!
MongoDB - Who, What & Where!MongoDB - Who, What & Where!
MongoDB - Who, What & Where!
 
Siber Güvenlik Eğitiminde Uluslararası CTF Yarışmaları
Siber Güvenlik Eğitiminde Uluslararası CTF YarışmalarıSiber Güvenlik Eğitiminde Uluslararası CTF Yarışmaları
Siber Güvenlik Eğitiminde Uluslararası CTF Yarışmaları
 
BGA CTF Ethical Hacking Yarışması Çözümleri
BGA CTF Ethical Hacking Yarışması ÇözümleriBGA CTF Ethical Hacking Yarışması Çözümleri
BGA CTF Ethical Hacking Yarışması Çözümleri
 

Ähnlich wie PHP Loves MongoDB - Dublin MUG (by Hannes)

Symfony2 and MongoDB
Symfony2 and MongoDBSymfony2 and MongoDB
Symfony2 and MongoDBPablo Godel
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011Steven Francia
 
Enyo for JS Nerds - Austin JS Meetup, April 2012
Enyo for JS Nerds - Austin JS Meetup, April 2012Enyo for JS Nerds - Austin JS Meetup, April 2012
Enyo for JS Nerds - Austin JS Meetup, April 2012Ben Combee
 
Mongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeMongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeSpyros Passas
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsMongoDB
 
PostgreSQLからMongoDBへ
PostgreSQLからMongoDBへPostgreSQLからMongoDBへ
PostgreSQLからMongoDBへBasuke Suzuki
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBAlex Bilbie
 
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hocCiconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hocCalvin Froedge
 
MongoSV Schema Workshop
MongoSV Schema WorkshopMongoSV Schema Workshop
MongoSV Schema WorkshopMongoDB
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introductionTse-Ching Ho
 
Mongo db basic installation
Mongo db basic installationMongo db basic installation
Mongo db basic installationKishor Parkhe
 
PhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHPPhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHPichikaway
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMetatagg Solutions
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - GuilinJackson Tian
 
This upload requires better support for ODP format
This upload requires better support for ODP formatThis upload requires better support for ODP format
This upload requires better support for ODP formatForest Mars
 

Ähnlich wie PHP Loves MongoDB - Dublin MUG (by Hannes) (20)

Symfony2 and MongoDB
Symfony2 and MongoDBSymfony2 and MongoDB
Symfony2 and MongoDB
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
 
Enyo for JS Nerds - Austin JS Meetup, April 2012
Enyo for JS Nerds - Austin JS Meetup, April 2012Enyo for JS Nerds - Austin JS Meetup, April 2012
Enyo for JS Nerds - Austin JS Meetup, April 2012
 
Mongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeMongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappe
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
PostgreSQLからMongoDBへ
PostgreSQLからMongoDBへPostgreSQLからMongoDBへ
PostgreSQLからMongoDBへ
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Hardcore PHP
Hardcore PHPHardcore PHP
Hardcore PHP
 
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hocCiconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
 
MongoSV Schema Workshop
MongoSV Schema WorkshopMongoSV Schema Workshop
MongoSV Schema Workshop
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
Mongo db basic installation
Mongo db basic installationMongo db basic installation
Mongo db basic installation
 
PhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHPPhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHP
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg Solutions
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
dojo.Patterns
dojo.Patternsdojo.Patterns
dojo.Patterns
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
 
This upload requires better support for ODP format
This upload requires better support for ODP formatThis upload requires better support for ODP format
This upload requires better support for ODP format
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 

Kürzlich hochgeladen

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
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
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
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
 
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
 
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
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"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
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
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
 
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
 
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
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 

Kürzlich hochgeladen (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
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
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
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
 
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
 
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
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"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
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
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
 
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
 
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
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 

PHP Loves MongoDB - Dublin MUG (by Hannes)

  • 2. • Hannes / @bjori • Icelandic » Oslo, Norway » London, England • PHP Driver Engineer » @10gen • PHP Contributor About me 2 Saturday 18 August 2012
  • 3. Why I started with MongoDB • Like creating (code, not graphics) • Productivity • Don’t want to waste my time 3 Saturday 18 August 2012
  • 6. Terminology RDBMS Mongo Table, View ➜ Collection Row ➜ JSON Document Index ➜ Index Join ➜ Embedded Document Partition ➜ Shard Partition Key ➜ Shard Key 6 Saturday 18 August 2012
  • 7. PostObject Object( [title] => MongoDB [contributors] => Array( [0] => PersonObject Object( [name] => Eliot Horowitz [email] => eh@10gen.com ) [1] => PersonObject Object( [name] => Dwight Merriman [email] => dm@10gen.com ) ) [model] => ModelObject Object( [relational] => [awesome] => 1 ) ) Tables to Documents { title: ‘MongoDB’, contributors: [ { name: ‘Eliot Horowitz’, email: ‘eh@10gen.com’ }, { name: ‘Dwight Merriman’, email: ‘dm@10gen.com’ } ], model: { relational: false, awesome: true } } Documents to Arrays Documents to Objects 7 Saturday 18 August 2012
  • 8. PostObject Object( [title] => MongoDB [contributors] => Array( [0] => PersonObject Object( [name] => Eliot Horowitz [email] => eh@10gen.com ) [1] => PersonObject Object( [name] => Dwight Merriman [email] => dm@10gen.com ) ) [model] => ModelObject Object( [relational] => [awesome] => 1 ) ) array( "title" => 'MongoDB', "contributors" => array( array( 'name' => 'Eliot Horowitz', 'email' => 'eh@10gen.com' ), array( 'name' => 'Dwight Merriman', 'email' => 'dm@10gen.com' ) ), "model" => array( 'relational' => false, 'awesome' => true Tables to Documents { title: ‘MongoDB’, contributors: [ { name: ‘Eliot Horowitz’, email: ‘eh@10gen.com’ }, { name: ‘Dwight Merriman’, email: ‘dm@10gen.com’ } ], model: { relational: false, awesome: true } } Documents to Arrays Documents to Objects 7 Saturday 18 August 2012
  • 9. PostObject Object( [title] => MongoDB [contributors] => Array( [0] => PersonObject Object( [name] => Eliot Horowitz [email] => eh@10gen.com ) [1] => PersonObject Object( [name] => Dwight Merriman [email] => dm@10gen.com ) ) [model] => ModelObject Object( [relational] => [awesome] => 1 ) ) array( "title" => 'MongoDB', "contributors" => array( array( 'name' => 'Eliot Horowitz', 'email' => 'eh@10gen.com' ), array( 'name' => 'Dwight Merriman', 'email' => 'dm@10gen.com' ) ), "model" => array( 'relational' => false, 'awesome' => true Tables to Documents { title: ‘MongoDB’, contributors: [ { name: ‘Eliot Horowitz’, email: ‘eh@10gen.com’ }, { name: ‘Dwight Merriman’, email: ‘dm@10gen.com’ } ], model: { relational: false, awesome: true } } Documents to Arrays Documents to Objects 7 Saturday 18 August 2012
  • 11. PHP Driver No dependencies https://github.com/mongodb/mongo-php-driver/downloads Maintained and supported by 10gen $ pecl install mongo extension = mongo.so 9 Saturday 18 August 2012
  • 12. Connecting to the DB If the DB or collection doesn’t exist, mongoDB will create it on connect. $connection = new Mongo(); $db = $connection->selectDB('blog'); $posts = $db->post; 10 mongo.default_host = localhost mongo.default_port = 27017 Saturday 18 August 2012
  • 13. Connecting to ReplicaSet If one of the servers is down, we’ll connect to the next one Auto-discovery of the RS topology $connection = new Mongo('server1,server2', array(‘replicaSet’ => ‘myReplicaSet’)); $db = $connection->selectDB('blog'); $posts = $db->post; 11 Saturday 18 August 2012
  • 14. Documents Blog Post Document $p1 = array("author" => "roger", "date" => new MongoDate(), "text" => "about mongoDB...", "tags" => array('tech', 'databases'), ); $posts->insert($p1); 12 Saturday 18 August 2012
  • 15. Silly Billy No error check! try { $posts->insert($p1, array("safe" => true)); catch( Exception $e ) { echo $e->getMessage(); /* ... */ } 13 Saturday 18 August 2012
  • 16. Querying Note: _id is unique, but can be anything you’d like > db.posts.findOne() > { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ] } print_r($posts->findOne()); print_r($p1); Array( [_id] => MongoId Object( [$id] => 4e9796764a18173a17000000 ) [author] => roger [date] => MongoDate Object( [sec] => 1318557302 [usec] => 581000 ) [text] => about mongoDB... [tags] => Array( [0] => tech [1] => databases ) ) 14 Saturday 18 August 2012
  • 17. Querying Note: _id is unique, but can be anything you’d like > db.posts.findOne() > { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ] } print_r($posts->findOne()); print_r($p1); Array( [_id] => MongoId Object( [$id] => 4e9796764a18173a17000000 ) [author] => roger [date] => MongoDate Object( [sec] => 1318557302 [usec] => 581000 ) [text] => about mongoDB... [tags] => Array( [0] => tech [1] => databases ) ) 14 Saturday 18 August 2012
  • 18. MongoId An autogenerated primary key $x = array("foo" => 1, "bar" => 2); $db->baz->insert($x); var_dump($x['_id']); Result: object(MongoId)#4 (1) { ["$id"]=> string(24) "4e9cc76a4a1817fd21000000" } 15 Saturday 18 August 2012
  • 19. MongoId An autogenerated primary key object(MongoId)#4 (1) { ["$id"]=> string(24) "4e9cc76a4a1817fd21000000" } 12bytes 4e9cc76a4a1817fd21000000 |------||----||--||----| ts machine pid inc 16 Saturday 18 August 2012
  • 20. Update Operations $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit $change = array('$push' => array('comments' => array( 'author' => 'Fred', 'created' => new MongoDate(), 'comment' => 'Best post ever!.' ) ) ); $posts->update(array("_id" => $id), $change); 17 Saturday 18 August 2012
  • 21. Array( [_id] => MongoId Object ( [$id] => 4e9796764a18173a17000000 ) [author] => roger [comments] => Array( [0] => Array( [author] => Fred [created] => MongoDate Object( [sec] => 1318899392 [usec] => 176000 ) [comment] => Dumb post. ) ) [date] => MongoDate Object( [sec] => 1318557302 [usec] => 581000 ) [tags] => Array( [0] => tech [1] => databases ) [text] => about mongoDB... ) Nested Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : ISODate("2012-08-13T11:11:00.294Z"), text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [ { author : "Fred", created : ISODate("2012-08-13T11:41:00.634Z"), comment : "Best Post Ever!" } ] } 18 Saturday 18 August 2012
  • 22. Array( [_id] => MongoId Object ( [$id] => 4e9796764a18173a17000000 ) [author] => roger [comments] => Array( [0] => Array( [author] => Fred [created] => MongoDate Object( [sec] => 1318899392 [usec] => 176000 ) [comment] => Dumb post. ) ) [date] => MongoDate Object( [sec] => 1318557302 [usec] => 581000 ) [tags] => Array( [0] => tech [1] => databases ) [text] => about mongoDB... ) Nested Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : ISODate("2012-08-13T11:11:00.294Z"), text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [ { author : "Fred", created : ISODate("2012-08-13T11:41:00.634Z"), comment : "Best Post Ever!" } ] } 18 Saturday 18 August 2012
  • 23. Querying $posts = $blog->find(array( "author" => "Roger")); $commentsByFred = $blog->find(array( "comments.author" => "Fred")); $commentsByFred = $blog->find(array( "comments.author" => new MongoRegex("/fred/i"))); 19 Saturday 18 August 2012
  • 24. It’s all about the $ $ instead of >, <, =, etc. $gt, $gte, $lt, $lte, $eq, $neq, $exists, $set, $mod, $where, $in, $nin, $inc $push, $pull, $pop, $pushAll, $popAll $c->find(array("x" => array('$gt' => 4))); $c->find(array("x" => array(“$gt” => 4))); 20 Saturday 18 August 2012
  • 25. It’s all about the $ $ instead of >, <, =, etc. $gt, $gte, $lt, $lte, $eq, $neq, $exists, $set, $mod, $where, $in, $nin, $inc $push, $pull, $pop, $pushAll, $popAll $c->find(array("x" => array('$gt' => 4))); $c->find(array("x" => array(“$gt” => 4))); 20 Saturday 18 August 2012
  • 26. Adjust the behavior in php.ini or using ini_set() php.ini --------------- mongo.cmd = ":" $c->find(array("x" => array(':gt' => 4))); or ini_set("mongo.cmd", "."); $c->find(array("x" => array('.gt' => 4))); 21 Saturday 18 August 2012
  • 27. Indexing $people->ensureIndex(array("age" => 1)); $people->ensureIndex(array( "name" => -1, "ts" => -1, "comments.author" => 1 )); 22 Saturday 18 August 2012
  • 28. More Indexing // Index nested documents $posts->ensureIndex( array("comments.author" => 1) ) $posts->find( array("comments.author" => "Fred") ) // Index on tags (multi-key index) $posts->ensureIndex( array("tags" => 1 ) ) $posts->find( array("tags" => "tech") ) // geospatial index $posts->ensureIndex( array("author.location" => "2d") ) $posts->find( array("author.location" => array( '$near' => array(22,42))); 23 Saturday 18 August 2012
  • 29. Cursors $cursor = $c->find(array("foo" => "bar")) foreach ($cursor as $id => $value) { echo "$id: "; var_dump( $value ); } $a = iterator_to_array($cursor); 24 Saturday 18 August 2012
  • 30. Paging $page_num = 3; $results_per_page = 10; $cursor = $results->find() ->sort(array("ts" => -1)) ->skip($page_num * $results_per_page) ->limit($results_per_page); 25 Saturday 18 August 2012
  • 31. Rich Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), line_items : [ { sku: ‘tt-123’, name: ‘Coltrane: Impressions’ }, { sku: ‘tt-457’, name: ‘Davis: Kind of Blue’ } ], address : { name: ‘Banker’, street: ‘111 Main’, zip: 10010 }, payment: { cc: 4567, exp: Date(2011, 7, 7) }, subtotal: 2355 } 26 Saturday 18 August 2012
  • 32. Libraries & Frameworks • Doctrine MongoDB ODM • Zend Framework • Lithium • Symfony2 • Kohana • Drupal • Yii 27 Saturday 18 August 2012
  • 33. Doctrine MongoDB ODM • Provides full validation • Seamless integration with Doctrine ORM – Useful for hybrid solutions • Follows same persistence model as Doctrine • Uses a Document Manager & annotations • Supports embedded and referenced objects • Can use mongo’s query interface • Supports in place updates https://github.com/doctrine/mongodb-odm 28 Saturday 18 August 2012
  • 34. Grid FS Saturday 18 August 2012
  • 36. Storing Big Files >16mb stored in 16mb chunks Saturday 18 August 2012
  • 37. Storing Big Files Works with replicated and sharded systems Saturday 18 August 2012
  • 38. A better network FS • GridFS files are seamlessly sharded & replicated. • No OS constraints... • No file size limits • No naming constraints • No folder limits • Standard across different OSs • MongoDB automatically generate the MD5 hash of the file Saturday 18 August 2012
  • 39. Storing Files $grid = $db->getGridFS(); // The file's location in the File System $path = "/tmp/"; $filename = "movie.mp4"; // Note metadata field & filename field $storedfile = $grid->storeFile($path . $filename, array ("metadata" => array("filename" => $filename), "filename" => $filename)); // Return newly stored file's Document ID echo $storedfile; Saturday 18 August 2012
  • 40. Storing Data $grid = $db->getGridFS(); $filename = "zelda.jpg"; $storedfile = $grid->storeBytes("√ø√ò√ø√⠀ ^@^PJFIF^@^A^A^A,^G^E^E^G,...", array("metadata" => array("filename" => $filename), "filename" => $filename)); Saturday 18 August 2012
  • 41. Streaming Data $gridFS = $db->getGridFS(); // Find image to stream $image = $gridFS->findOne("zelda.jpg"); // Stream image to browser header('Content-type: image/jpeg'); echo $image->getBytes(); Saturday 18 August 2012
  • 42. PS: We’re hiring!! Contact us at jobs@10gen.com Questions? download at mongodb.org 37 Saturday 18 August 2012