Вы находитесь на странице: 1из 22

noSQL ‘r’DBMS

an Introduction
by
{ name: “adzmely mansor”,
email: “adzmely@gmail.com”,
event: “PHP Meetup 2010, Malaysia” }
what is MongoDB

MongoDB - “humongous” (Huge + Monstrous)


source: Merriam - Webster Dictionary

scalable, high-performance, open source


“document-oriented” database
written in C++
- http://mongodb.org
About MongoDB
like mySQL,postgresql,etc listen on port for
incoming connections
default 27017 : ./mongod --fork --logpath /var/log/mongodb.log --logappend

interactive shell like mysql/psql


./mongo dbname

> show dbs : display all databases

> use dbname : change db

> show collections : list all collections in current db


About MongoDB
Javascripting in interactive shell
./mongo dbname
> var a=1
> var b=10
> var c = a + b
> print ("This is the total of a + b =",c)
> for (i=0; i<10; i++) {
... print ("Hooray!!!");
... }

administrative maintenance will be easy

walk through records and via java scripting perform maintenance task

others
Modeling in MogoDB
Modeling in MogoDB

Collections and Documents


Collection is just like a table
in collection we have “documents”
a document is just like a row in table
the difference is that each “row” can contain other “row”
Modeling in MogoDB

Collections and Documents


Collection = Table
Document = Row/Record
Document = nDocument
or
same Row can have another Row(s)
Modeling in MogoDB
MongoDB is SchemaLess
- no schema language (create table, etc)
- create / add / remove key value store/document
dynamically
- data type = weakly typed (implicitly converts types
when used)

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

php.ini => extension=mongo.so / php_mongo.dll // *nix/windows

PHPMoAdmin
MongoDB administration tool for PHP

CakePHP datasource available

Doctrine Object Document Mapper (experimental)

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();

// DSN authentication with selected DB


$db = new Mongo(“mongodb://username:password@localhost/mydatabase”);

// DSN authentication without selected DB


$connection = new Mongo(“mongodb://username:password@localhost”);

// Selecting or dynamically create a new DB, if myDB exist = select, else create new
$db = $connection->myDB;

// Select or create another DB


$db = $connection->anotherDB;

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

// create an index on userid : 1 = ascending : -1 = descending


$collection->ensureIndex( array ( “userid” => 1 ) );

// select * from wall


$cursor = $collection->find();
// select * from wall where userid = ‘zam’
$query = array( “userid” => “zam”);
$cursor = $collection->find($query); // walk $cursor with foreach

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

// adding/insert a wall post


$wall = array(
“title” => “I’am a beginner in php can i join?”,
“userid” => “basyirah”,
“timestamp” => new MongoDate(),
“comments” => array()
);

$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()
);

// update using $push - modifier operation


$collection->update(
array(“_id” => $wall_id),
array(“$push” => array(“comments” => $newComment))
);

?>
Effective Programmer
Agile Approach
dynamic database creation

dynamic collection/table creation

schemaless , fields can easily push or pop in document(s)

Scalable
scale horizontally without compromising functionality

auto sharding - beta (production ready in v 1.6)

replication
Effective Programmer

Having the Right “Tool(s)”


for
the Right “Job(s)”
MongoDB in Production
allows users to shorten, share, and track links. uses MongoDB to
store user history

GitHub, the social coding site, is using MongoDB for an internal


reporting application

Boxed Ice, server monitoring solution, store 600 millions+


documents in MongoDB

Wordnik stores its entire text corpus in MongoDB - 1.2TB of


data in over 5 billion records
Thank You
Questions & Answers

{ name: “adzmely mansor”,


email: “adzmely@gmail.com”,
event: “PHP Meetup 2010, Malaysia” }
do you know that?

May 2010 - 2 Billions tweets / month


source : http://www.pingdom.com

migrate from MySQL


to
Cassandra
( an open source document store db under apache foundation )

Вам также может понравиться