Professional Documents
Culture Documents
mongoDB PHPMeetup2010
mongoDB PHPMeetup2010
an Introduction
by
{ name: “adzmely mansor”,
email: “adzmely@gmail.com”,
event: “PHP Meetup 2010, Malaysia” }
what is MongoDB
walk through records and via java scripting perform maintenance task
others
Modeling in MogoDB
Modeling in MogoDB
var x = 5; // x is an integer
var y = “37”; // y is a string
x+y; // “537” js will concatenate
// (5 converted to string)
Relational Model
Wall Post
- id
- title
- userid
- timestamp
Comments
- id
- wall_id (foreign key)
- userid
- comment
- timestamp
Document Stores
{
title: “i am beginner in php, can i join? or this kelas utk yg dah
hastle..?”,
userid: “basyirah”,
timestamp: "Thu Jun 24 2010 09:40:36 GMT+0800 (MYT)",
comments: [
{
userid: “hizam”,
comment: “please join, topic agak perlbagai dan
cover level basic sampai (maybe) advance....”,
timestamp: “Thu Jun 24 2010 09:45:36 GMT
+0800 (MYT)",
},
{
userid: “basyirah”,
comment: “ok, will do”,
timestamp: “Thu Jun 24 2010 09:50:36 GMT
+0800 (MYT)",
}
}
Document Stores
- JSON-Style documents with
dynamic schemas var w = {
title: “i am beginner in php, can i join? or this kelas utk yg dah
- indexes hastle..?”,
userid: “basyirah”,
- updates are atomic timestamp: "Thu Jun 24 2010 09:40:36 GMT+0800 (MYT)",
comments: [
{
// Add wall record userid: “hizam”,
db.wall.insert(w) comment: “please join, topic agak perlbagai dan
// Add new comment to wall post cover level basic sampai (maybe) advance....”,
db.wall.update ( timestamp: “Thu Jun 24 2010 09:45:36 GMT
{‘_id’: wall_id}, +0800 (MYT)",
{‘$push’: {‘comments’: },
{ {
‘userid’:‘hizam’, userid: “basyirah”,
‘comment’:‘Good!!!’, comment: “ok, will do”,
‘timestamp’: ‘Thus Jun 24 2010 09:50:36 timestamp: “Thu Jun 24 2010 09:50:36 GMT
GMT +800 (MYT)’ +0800 (MYT)",
}
}}
]
} );
// select first 5 comments };
db.wall.find({}, {comments:{$slice: 5}})
// select last 5 comments
db.wall.find({}, {comments:{$slice: -5}})
// skip first 20 limit 10
db.wall.find({}, {comments:{$slice: [20, 10]}})
// 20 from end limit 10
db.wall.find({}, {comments:{$slice: [-20, 10]}})
MongoDB and PHP
driver available in PECL
pecl install mongodb
PHPMoAdmin
MongoDB administration tool for PHP
Framework Integration
Drupal / Kohana / Symfony / Zend / etc
MongoDB and PHP
no new query language to learn!
send array and it returns array of documents
<?php
// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
?>
MongoDB and PHP
php example :: Connection & DB
<?php
// connects to localhost port 27017
$connection = new Mongo();
// Selecting or dynamically create a new DB, if myDB exist = select, else create new
$db = $connection->myDB;
?>
MongoDB and PHP
php example :: Collection , Index and Simple Query
<?php
$db = $connection->myDB;
// Select or create a collection or in other word a ‘table’
$collection = $db->wall;
// or simply
$collection = $connection->myDB->wall;
?>
MongoDB and PHP
php example :: Add/Insert a Document
<?php
$db = $connection->myDB;
// Select or create a collection or in other word a ‘table’
$collection = $db->wall;
$collection->insert($wall);
?>
MongoDB and PHP
php example :: Update a Document
<?php
$db = $connection->myDB;
$collection = $db->wall;
// new comment
$newComment = array(
“comment” => “Please join, topic agak perbagai”,
“userid” => “hizam”,
“timestamp” => new MongoDate()
);
?>
Effective Programmer
Agile Approach
dynamic database creation
Scalable
scale horizontally without compromising functionality
replication
Effective Programmer