Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
MongoDB Advanced Topics
          César D. Rodas
           crodas@php.net
         http://crodas.org/




     Yahoo! Ope...
Who is this fellow?
         Paraguayan
         Zealot of
              • Open Source
              • PHP
              •...
I'd like to thanks to...


         10gen

         Yahoo!

         My Brazilian friends




@crodas - http://crodas.org/...
Agenda
         Introduction to MongoDB
         MongoDB Queries
         Real life example:
              • Designing dat...
MongoDB
         <EN>Mongo</EN> != <PT>Mongo</PT>
         Document oriented database
         Fast, Scalable, Easy to use...
Documents?



@crodas - http://crodas.org/ - L EX
                               AT                  6
It's just an Array()



@crodas - http://crodas.org/ - L EX
                               AT                7
In fact everything is an
                 Array() in MongoDB


@crodas - http://crodas.org/ - L EX
                       ...
http://bit.ly/mongodb-php



@crodas - http://crodas.org/ - L EX
                               AT     9
The fun part



@crodas - http://crodas.org/ - L EX
                               AT                    10
MongoDB - Operations
         Select
              • $gt, $lt, $gte, $lte, $eq, $neq: >, <, >=, <=, ==, !=
              •...
pecl install mongo



@crodas - http://crodas.org/ - L EX
                               AT              12
MongoDB - Connection


/* connects to localhost:27017 */
$connection = new Mongo();

/* connect to a remote host (default ...
FROM SQL to MongoDB




@crodas - http://crodas.org/ - L EX
                               AT      14
MongoDB - Count

/* SELECT count(*) FROM table */
$collection->count();

/* SELECT count(*) FROM table WHERE foo = 1 */
$c...
MongoDB - Queries
/*
 * SELECT * FROM table WHERE field IN (5,6,7) and enable=1
 * and worth < 5
 * ORDER BY timestamp DES...
MongoDB - Pagination
/*
 * SELECT * FROM table WHERE field IN (5,6,7) and enable=1
 * and worth < 5
 * ORDER BY timestamp ...
Designing data structure
                                      Simple multi-blog system




@crodas - http://crodas.org/ -...
MongoDB - Collections
         Blog
         Post
         User
         Comment




@crodas - http://crodas.org/ - L EX
 ...
Blog document
$blog = array(
   "user" => <userref>,
   "title" => "Foo bar blog"
   "url" => array(
       "foobar.foobar...
Post document
$post = array(
   "blog" => <blogref>,
   "author" => <userref>,
   "uri" => "/another-post",
   "title" => ...
Missing docs.
$comment = array(
   "post" => <postref>,
   "name" => "foobar",
   "email" => "foo@bar.com",
   "comment" =...
About indexes

$blog col->ensureIndex(array("url" => 1));

$post col->ensureIndex(array("blog" => 1, "date" => -1));
$post...
That's it.




@crodas - http://crodas.org/ - L EX
                               AT                  24
Hum...




@crodas - http://crodas.org/ - L EX
                               AT              25
Until now, nothing new




@crodas - http://crodas.org/ - L EX
                               AT      26
Let's shard it!




@crodas - http://crodas.org/ - L EX
                               AT                       27
Strategy
         Shard Blog collection
         Shard User collection
         Other collections are isolated
           ...
Configuring the sharding

/* Assuming you have already MongoDB running
 * on a sharded env., you should do this once.
 *
 ...
New blog document
$blog = array(
   "user" => <userref>,
   "title" => "Foo bar blog"
   "url" => array(
       "foobar.fo...
Select the DB and namespace
 to use "post" and "comments"



@crodas - http://crodas.org/ - L EX
                         ...
Questions?




@crodas - http://crodas.org/ - L EX
                               AT                  32
Thank you hackers!




@crodas - http://crodas.org/ - L EX
                               AT           33
MongoDB is looking from
                Brazilian help.



@crodas - http://crodas.org/ - L EX
                           ...
@crodas

                                      crodas.org



@crodas - http://crodas.org/ - L EX
                         ...
Upcoming SlideShare
Loading in …5
×

MongoDB Advanced Topics

11,165 views

Published on

Published in: Technology
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

MongoDB Advanced Topics

  1. 1. MongoDB Advanced Topics César D. Rodas crodas@php.net http://crodas.org/ Yahoo! Open Hack Day 2010 São Paulo, Brasil 1
  2. 2. Who is this fellow? Paraguayan Zealot of • Open Source • PHP • MongoDB PECL Developer ... and few other things @crodas - http://crodas.org/ - L EX AT 2
  3. 3. I'd like to thanks to... 10gen Yahoo! My Brazilian friends @crodas - http://crodas.org/ - L EX AT 3
  4. 4. Agenda Introduction to MongoDB MongoDB Queries Real life example: • Designing data model for a Wordpress.com like site • Optimize our data model to run in sharded environment @crodas - http://crodas.org/ - L EX AT 4
  5. 5. MongoDB <EN>Mongo</EN> != <PT>Mongo</PT> Document oriented database Fast, Scalable, Easy to use Support Indexes • Simple • Compound • Geo spatial Support sharding PECL client @crodas - http://crodas.org/ - L EX AT 5
  6. 6. Documents? @crodas - http://crodas.org/ - L EX AT 6
  7. 7. It's just an Array() @crodas - http://crodas.org/ - L EX AT 7
  8. 8. In fact everything is an Array() in MongoDB @crodas - http://crodas.org/ - L EX AT 8
  9. 9. http://bit.ly/mongodb-php @crodas - http://crodas.org/ - L EX AT 9
  10. 10. The fun part @crodas - http://crodas.org/ - L EX AT 10
  11. 11. MongoDB - Operations Select • $gt, $lt, $gte, $lte, $eq, $neq: >, <, >=, <=, ==, != • $in, $nin • $size, $exists • $where: Any javascript expression • group() • limit() • skip() Update • $set • $unset • $push • $pull • $inc @crodas - http://crodas.org/ - L EX AT 11
  12. 12. pecl install mongo @crodas - http://crodas.org/ - L EX AT 12
  13. 13. MongoDB - Connection /* connects to localhost:27017 */ $connection = new Mongo(); /* connect to a remote host (default port) */ $connection = new Mongo( "example.com" ); /* connect to a remote host at a given port */ $connection = new Mongo( "example.com:65432" ); /* select some DB (and create if it doesn't exits yet) */ $db = $connection->selectDB("db name"); /* select a "table" (collection) */ $table = $db->getCollection("table"); @crodas - http://crodas.org/ - L EX AT 13
  14. 14. FROM SQL to MongoDB @crodas - http://crodas.org/ - L EX AT 14
  15. 15. MongoDB - Count /* SELECT count(*) FROM table */ $collection->count(); /* SELECT count(*) FROM table WHERE foo = 1 */ $collection->find(array("foo" => 1))->count(); @crodas - http://crodas.org/ - L EX AT 15
  16. 16. MongoDB - Queries /* * SELECT * FROM table WHERE field IN (5,6,7) and enable=1 * and worth < 5 * ORDER BY timestamp DESC */ $collection->ensureIndex( array('field'=>1, 'enable'=>1, 'worth'=>1, 'timestamp'=>-1) ); $filter = array( 'field' => array('$in' => array(5,6,7)), 'enable' => 1, 'worth' => array('$lt' => 5) ); $results = $collection->find($filter)->sort(array('timestamp' => -1)); @crodas - http://crodas.org/ - L EX AT 16
  17. 17. MongoDB - Pagination /* * SELECT * FROM table WHERE field IN (5,6,7) and enable=1 * and worth < 5 * ORDER BY timestamp DESC LIMIT $offset, 20 */ $filter = array( 'field' => array('$in' => array(5,6,7)), 'enable' => 1, 'worth' => array('$lt' => 5) ); $cursor = $collection->find($filter); $cursor->sort(array('timestamp' => -1))->skip($offset)->limit(20); foreach ($cursor as $result) { var dump($result); } @crodas - http://crodas.org/ - L EX AT 17
  18. 18. Designing data structure Simple multi-blog system @crodas - http://crodas.org/ - L EX AT 18
  19. 19. MongoDB - Collections Blog Post User Comment @crodas - http://crodas.org/ - L EX AT 19
  20. 20. Blog document $blog = array( "user" => <userref>, "title" => "Foo bar blog" "url" => array( "foobar.foobar.com", "anotherfoo.foobar.com", ), "permissions" => array( "edit" => array(<userref>, <userref>) ); "post" => 1, ... ); @crodas - http://crodas.org/ - L EX AT 20
  21. 21. Post document $post = array( "blog" => <blogref>, "author" => <userref>, "uri" => "/another-post", "title" => "Another post", "excerpt" => "bla, bla, bla", "body" => "bar, foo bar, foo, bar", "tags" => array("list", "of tags"), "published" => true, "date" => <mongodate>, ); @crodas - http://crodas.org/ - L EX AT 21
  22. 22. Missing docs. $comment = array( "post" => <postref>, "name" => "foobar", "email" => "foo@bar.com", "comment" => "Hello world", "date" => <mongodate>, ); $user = array( "user" => "crodas", "email" => "crodas@php.net", "password" => "a1bad96dc68f891ca887d50eb3fb65ec82123d05", "name" => "Cesar Rodas", ); @crodas - http://crodas.org/ - L EX AT 22
  23. 23. About indexes $blog col->ensureIndex(array("url" => 1)); $post col->ensureIndex(array("blog" => 1, "date" => -1)); $post col->ensureIndex(array("blog" => 1, "uri" => 1), array("unique" => 1)); $comment col->ensureIndex(array("post" => 1, "date" => -1)); $user col->ensureIndex(array("user" => 1), array("unique" => 1)); $user col->ensureIndex(array("email" => 1), array("unique" => 1)); @crodas - http://crodas.org/ - L EX AT 23
  24. 24. That's it. @crodas - http://crodas.org/ - L EX AT 24
  25. 25. Hum... @crodas - http://crodas.org/ - L EX AT 25
  26. 26. Until now, nothing new @crodas - http://crodas.org/ - L EX AT 26
  27. 27. Let's shard it! @crodas - http://crodas.org/ - L EX AT 27
  28. 28. Strategy Shard Blog collection Shard User collection Other collections are isolated • Create namespaces for post and comments (foo.post, foo.comments) • Modify Blog collection, add info about "namespace" (and DB). MongoDB will distribute our DB and namespaces @crodas - http://crodas.org/ - L EX AT 28
  29. 29. Configuring the sharding /* Assuming you have already MongoDB running * on a sharded env., you should do this once. * * $admin is a connection to the "admin" DB. */ $admin->command(array( "shardcollection" => "blog", "key" => array("uri" => 1), )); $admin->command(array( "shardcollection" => "user", "key" => array("user" => 1), "unique" => true, )); @crodas - http://crodas.org/ - L EX AT 29
  30. 30. New blog document $blog = array( "user" => <userref>, "title" => "Foo bar blog" "url" => array( "foobar.foobar.com", "anotherfoo.foobar.com", ), "permissions" => array( "edit" => array(<userref>, <userref>) ); "post" => 1, "namespace" => "3h3g3k3", "database" => "34fs321", ); @crodas - http://crodas.org/ - L EX AT 30
  31. 31. Select the DB and namespace to use "post" and "comments" @crodas - http://crodas.org/ - L EX AT 31
  32. 32. Questions? @crodas - http://crodas.org/ - L EX AT 32
  33. 33. Thank you hackers! @crodas - http://crodas.org/ - L EX AT 33
  34. 34. MongoDB is looking from Brazilian help. @crodas - http://crodas.org/ - L EX AT 34
  35. 35. @crodas crodas.org @crodas - http://crodas.org/ - L EX AT 35

×