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
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
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