MongoDB For Perl                      Developers                      { author: “Ynon Perek” }Friday, March 1, 13
Whoami                      Ynon Perek                      http://ynonperek.com                      ynon@ynonperek.comFr...
Agenda                           Mongo Is Awesome                           CRUD Operations                           The ...
Mongo Is Awesome                      Data Store for                      JSON ObjectsFriday, March 1, 13
Mongo Is Awesome                      Data Store for                      JSON Objects          {                “Name” : ...
JSON Objects                      A JSON Object is a                      collection of key/                      value pa...
It’s A Document Oriented Data                      StoreFriday, March 1, 13
It don’t do joinsFriday, March 1, 13
It don’t do transactionsFriday, March 1, 13
Keeping It Simple                      Document Oriented                      No Transactions                      No Join...
Application Architecture                        APP            DBFriday, March 1, 13
What Can Mongo Do For You                      Create and store objects                      Arrange them in collections  ...
Q&AFriday, March 1, 13
CRUD Operations                      Create, Read, Update and Destroy DataFriday, March 1, 13
Mongo CRUD                      Create   is called insert                      Read     is called find                    ...
Mongo CRUD                      db.highscore.insert ({"name":"Tom", "score":94});                      db.highscore.find  ...
Inserting Data                      Use the command insert or save to insert a new object                      db.collecti...
Inserting Data                      Inserting to a new collection creates the collection                      Inserting an...
Demo: InsertFriday, March 1, 13
Reading Data                      find and findOne perform read operations                      Both take a query         ...
Query Document                      An empty (or missing) query document returns                      everything          ...
Query Document                      Each key/value pair in the query document imposes a                      condition on ...
Query Document                                                            Query Object                      Each key/value...
Query Document                      A compound query means a logical AND on the                      conditions.          ...
Quiz: What Is Returned                                          from      alterego   publisher                            ...
Quiz: What Is Returned                                          from      alterego   publisher                      {     ...
Resources                      Queries Cheat Sheet                      http://www.10gen.com/sites/default/files/downloads...
Demo: QueryFriday, March 1, 13
Update                      The general form for update is:                      db.collection.update(                    ...
Update                      Some Update Operators                        “$set”, “$inc”                        “$push”, “$...
Update: set                      $set modifies a value or add a new value                      Example:                   ...
Quiz: $set                      Update owners array of the first cat with white color                      If you want to ...
Deleting Data                      db.posts.remove(                              { “author” : “Father Angelo” })          ...
Q&AFriday, March 1, 13
The Driver                      Hello MongoDB                      Connecting and Authenticating                      Quer...
Hello MongoDB                      Mike Friedman, Kristina                      Chodorow and rafl                      Mon...
Hello Mongo              use strict;              use warnings;              use v5.14;              use MongoDB;         ...
Inserting Data              use strict;              use warnings;              use v5.14;              use MongoDB;      ...
Querying Data              my $cursor = $coll->find({name => Tom});                             while ( my $next = $cursor...
Querying Data                      Sort, Limit and Skip results using the cursor              my $cursor = $coll->find()->...
Update Data                      $coll->update( { name => Tom },                                     { $inc => { score => ...
Deleting Data                             my $client = MongoDB::MongoClient->new;              my $db = $client->get_datab...
Coding Time                      Mongo Log Parsing                      Mongo Web Address                      Book       ...
Thanks For Listening                      Ynon Perek                      Slides at:                      ynonperek.com   ...
Upcoming SlideShare
Loading in …5
×

MongoDB for Perl Developers

5,013 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,013
On SlideShare
0
From Embeds
0
Number of Embeds
1,518
Actions
Shares
0
Downloads
39
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

MongoDB for Perl Developers

  1. 1. MongoDB For Perl Developers { author: “Ynon Perek” }Friday, March 1, 13
  2. 2. Whoami Ynon Perek http://ynonperek.com ynon@ynonperek.comFriday, March 1, 13
  3. 3. Agenda Mongo Is Awesome CRUD Operations The Driver Coding TimeFriday, March 1, 13
  4. 4. Mongo Is Awesome Data Store for JSON ObjectsFriday, March 1, 13
  5. 5. Mongo Is Awesome Data Store for JSON Objects { “Name” : “Rose Tyler” }Friday, March 1, 13
  6. 6. JSON Objects A JSON Object is a collection of key/ value pairs {   "name" : "Rose Tyler", Keys are simple   "race" : "Human", strings   "body parts" : [ "head", "legs"] } Values can be: Numbers, Strings, Arrays, Other Objects, and moreFriday, March 1, 13
  7. 7. It’s A Document Oriented Data StoreFriday, March 1, 13
  8. 8. It don’t do joinsFriday, March 1, 13
  9. 9. It don’t do transactionsFriday, March 1, 13
  10. 10. Keeping It Simple Document Oriented No Transactions No JoinsFriday, March 1, 13
  11. 11. Application Architecture APP DBFriday, March 1, 13
  12. 12. What Can Mongo Do For You Create and store objects Arrange them in collections Retrieve them laterFriday, March 1, 13
  13. 13. Q&AFriday, March 1, 13
  14. 14. CRUD Operations Create, Read, Update and Destroy DataFriday, March 1, 13
  15. 15. Mongo CRUD Create is called insert Read is called find Update is called update Destroy is called removeFriday, March 1, 13
  16. 16. Mongo CRUD db.highscore.insert ({"name":"Tom", "score":94}); db.highscore.find ({"name" : "Tom" }) db.highscore.update ({"name" : "Tom"}, {"$inc" : { "score" : 1 } }); db.highscore.remove ({"name" : "Tom"});Friday, March 1, 13
  17. 17. Inserting Data Use the command insert or save to insert a new object db.collection.insert( obj ); db.collection.insert( array );Friday, March 1, 13
  18. 18. Inserting Data Inserting to a new collection creates the collection Inserting an object with an _id key, it is used as the object’s id (and must be unique).Friday, March 1, 13
  19. 19. Demo: InsertFriday, March 1, 13
  20. 20. Reading Data find and findOne perform read operations Both take a query find returns a cursor findOne returns an object Optional: Fields to fetch db.collection.find( <query>, <projection> )Friday, March 1, 13
  21. 21. Query Document An empty (or missing) query document returns everything db.collection.find({}) db.collection.find()Friday, March 1, 13
  22. 22. Query Document Each key/value pair in the query document imposes a condition on the results (objects that match). db.movies.find({ “genre” : “indie” }); db.books.find({“pages” : { “$gt” : 100 }});Friday, March 1, 13
  23. 23. Query Document Query Object Each key/value pair in the query document imposes a condition on the results (objects that match). db.movies.find({ “genre” : “indie” }); db.books.find({“pages” : { “$gt” : 100 }});Friday, March 1, 13
  24. 24. Query Document A compound query means a logical AND on the conditions. db.inventory.find(  {     “type” : “snacks”,     “available” : { “$lt” : 10 }  });Friday, March 1, 13
  25. 25. Quiz: What Is Returned from alterego publisher Bruce { Earth DC Wayne “publisher” : “DC” } Peter Earth Marvel Parker Krypton Clark Kent DCFriday, March 1, 13
  26. 26. Quiz: What Is Returned from alterego publisher { Bruce “publisher” : Earth DC Wayne “DC”, “from” : “Earth” Peter Earth Marvel } Parker Krypton Clark Kent DCFriday, March 1, 13
  27. 27. Resources Queries Cheat Sheet http://www.10gen.com/sites/default/files/downloads/ mongodb_qrc_queries.pdfFriday, March 1, 13
  28. 28. Demo: QueryFriday, March 1, 13
  29. 29. Update The general form for update is: db.collection.update( <query>, <update>, <options> ) Which Entries What to do with to update themFriday, March 1, 13
  30. 30. Update Some Update Operators “$set”, “$inc” “$push”, “$pushAll”, “$addToSet” “$pop”, “$pull”, “$pullAll”Friday, March 1, 13
  31. 31. Update: set $set modifies a value or add a new value Example: db.posts.update(  { title: “Why Is Your Cat Unhappy” },  { $set : { “archived” : true } } );Friday, March 1, 13
  32. 32. Quiz: $set Update owners array of the first cat with white color If you want to update all objects, use multi db.cats.update( { color: “white” }, { “$set” : { “owners” : [“John”, “Jim”] } } { multi : true } );Friday, March 1, 13
  33. 33. Deleting Data db.posts.remove( { “author” : “Father Angelo” }) db.music.remove({ “genres” : “pop” }) db.posts.remove({ “tags” : “funny” }, 1);Friday, March 1, 13
  34. 34. Q&AFriday, March 1, 13
  35. 35. The Driver Hello MongoDB Connecting and Authenticating Querying/Updating Data Coding TimeFriday, March 1, 13
  36. 36. Hello MongoDB Mike Friedman, Kristina Chodorow and rafl MongoDB::Examples MongoDB::Tutorial Alternative Driver: MangoFriday, March 1, 13
  37. 37. Hello Mongo use strict; use warnings; use v5.14; use MongoDB; use Data::Printer;   my $client = MongoDB::MongoClient->new; my $db = $client->get_database(demo);   my $coll = $db->get_collection(highscore); my @objects = $coll->find->all;   p @objects;Friday, March 1, 13
  38. 38. Inserting Data use strict; use warnings; use v5.14; use MongoDB;   my $client = MongoDB::MongoClient->new; my $db = $client->get_database(demo);   my $coll = $db->get_collection(highscore);   $coll->insert({ name => Mike, score => 99 });Friday, March 1, 13
  39. 39. Querying Data my $cursor = $coll->find({name => Tom});   while ( my $next = $cursor->next ) {   say $next->{name}, " Got: ", $next->{score}; }Friday, March 1, 13
  40. 40. Querying Data Sort, Limit and Skip results using the cursor my $cursor = $coll->find()-> sort({score => -1})-> limit(3);   while ( my $next = $cursor->next ) {   say $next->{name}, " Got: ", $next->{score}; }Friday, March 1, 13
  41. 41. Update Data $coll->update( { name => Tom }, { $inc => { score => 1 } } ); $coll->update( { name => Tom }, { $inc => { score => 1 } }, { multiple => 1 } );Friday, March 1, 13
  42. 42. Deleting Data   my $client = MongoDB::MongoClient->new; my $db = $client->get_database(demo);   my $coll = $db->get_collection(highscore);   $coll->remove( { score => { $gt => 80 } } );Friday, March 1, 13
  43. 43. Coding Time Mongo Log Parsing Mongo Web Address Book Code Examples At: https://github.com/ ynonp/perl- workshop-2013Friday, March 1, 13
  44. 44. Thanks For Listening Ynon Perek Slides at: ynonperek.com Talk to me at: ynon@ynonperek.comFriday, March 1, 13

×