SlideShare a Scribd company logo
MongoDB For Perl
                      Developers
                      { author: “Ynon Perek” }




Friday, March 1, 13
Whoami




                      Ynon Perek

                      http://ynonperek.com

                      ynon@ynonperek.com




Friday, March 1, 13
Agenda



                           Mongo Is Awesome

                           CRUD Operations

                           The Driver

                           Coding Time




Friday, March 1, 13
Mongo Is Awesome



                      Data Store for
                      JSON Objects




Friday, March 1, 13
Mongo Is Awesome



                      Data Store for
                      JSON Objects


          {
                “Name” : “Rose Tyler”
          }




Friday, March 1, 13
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 more




Friday, March 1, 13
It’s A Document Oriented Data
                      Store




Friday, March 1, 13
It don’t do joins




Friday, March 1, 13
It don’t do transactions




Friday, March 1, 13
Keeping It Simple




                      Document Oriented

                      No Transactions

                      No Joins




Friday, March 1, 13
Application Architecture




                        APP            DB




Friday, March 1, 13
What Can Mongo Do For You




                      Create and store objects

                      Arrange them in collections

                      Retrieve them later




Friday, March 1, 13
Q&A




Friday, March 1, 13
CRUD Operations
                      Create, Read, Update and Destroy Data


Friday, March 1, 13
Mongo CRUD



                      Create   is called insert

                      Read     is called find

                      Update is called update

                      Destroy is called remove




Friday, March 1, 13
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
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
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
Demo: Insert




Friday, March 1, 13
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
Query Document



                      An empty (or missing) query document returns
                      everything

                      db.collection.find({})

                      db.collection.find()




Friday, March 1, 13
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
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
Query Document


                      A compound query means a logical AND on the
                      conditions.

                      db.inventory.find(
                        {
                           “type” : “snacks”,
                           “available” : { “$lt” : 10 }
                        });




Friday, March 1, 13
Quiz: What Is Returned


                                          from      alterego   publisher

                                                    Bruce
                      {                   Earth                DC
                                                    Wayne
                          “publisher” :
                          “DC”
                      }                             Peter
                                          Earth                Marvel
                                                    Parker

                                          Krypton   Clark Kent DC



Friday, March 1, 13
Quiz: What Is Returned


                                          from      alterego   publisher

                      {                             Bruce
                          “publisher” :   Earth                DC
                                                    Wayne
                          “DC”,
                          “from” :
                          “Earth”                   Peter
                                          Earth                Marvel
                      }                             Parker

                                          Krypton   Clark Kent DC



Friday, March 1, 13
Resources




                      Queries Cheat Sheet
                      http://www.10gen.com/sites/default/files/downloads/
                      mongodb_qrc_queries.pdf




Friday, March 1, 13
Demo: Query




Friday, March 1, 13
Update




                      The general form for update is:


                      db.collection.update(
                        <query>, <update>, <options> )



               Which Entries                   What to do with
               to update                       them

Friday, March 1, 13
Update



                      Some Update Operators

                        “$set”, “$inc”

                        “$push”, “$pushAll”, “$addToSet”

                        “$pop”, “$pull”, “$pullAll”




Friday, March 1, 13
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
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
Deleting Data



                      db.posts.remove(
                              { “author” : “Father Angelo” })

                      db.music.remove({ “genres” : “pop” })

                      db.posts.remove({ “tags” : “funny” }, 1);




Friday, March 1, 13
Q&A




Friday, March 1, 13
The Driver



                      Hello MongoDB

                      Connecting and Authenticating

                      Querying/Updating Data

                      Coding Time




Friday, March 1, 13
Hello MongoDB



                      Mike Friedman, Kristina
                      Chodorow and rafl

                      MongoDB::Examples

                      MongoDB::Tutorial

                      Alternative Driver:
                      Mango




Friday, March 1, 13
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
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
Querying Data



              my $cursor = $coll->find({name => 'Tom'});
               
              while ( my $next = $cursor->next ) {
                say $next->{name}, " Got: ", $next->{score};
              }




Friday, March 1, 13
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
Update Data


                      $coll->update( { name => 'Tom' },
                                     { '$inc' => { score => 1 } } );



                      $coll->update( { name => 'Tom' },
                                     { '$inc' => { score => 1 } },
                                     { multiple => 1 } );




Friday, March 1, 13
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
Coding Time


                      Mongo Log Parsing

                      Mongo Web Address
                      Book

                      Code Examples At:
                      https://github.com/
                      ynonp/perl-
                      workshop-2013




Friday, March 1, 13
Thanks For Listening



                      Ynon Perek

                      Slides at:
                      ynonperek.com

                      Talk to me at:
                      ynon@ynonperek.com




Friday, March 1, 13

More Related Content

Similar to MongoDB for Perl Developers (20)

PDF
MongoDB For C++ Developers
Ynon Perek
 
PDF
Introduction To MongoDB
Ynon Perek
 
PDF
Mongo db basics
Claudio Montoya
 
PDF
MongoSV Schema Workshop
MongoDB
 
PDF
Building Apps with MongoDB
Nate Abele
 
PDF
MongoDB @ Frankfurt NoSql User Group
Chris Harris
 
PDF
De normalised london aggregation framework overview
Chris Harris
 
PPTX
Mongodb introduction and_internal(simple)
Kai Zhao
 
PDF
Schema Design
MongoDB
 
PDF
Building a MongoDB App with Perl
Mike Friedman
 
PPTX
Introduction to MongoDB
S.Shayan Daneshvar
 
PPTX
Mongo DB Presentation
Jaya Naresh Kovela
 
PPTX
Mongo db
Raghu nath
 
PDF
Mongo db php_shaken_not_stirred_joomlafrappe
Spyros Passas
 
PDF
Mongo db basics
Harischandra M K
 
PDF
Mongo db
Toki Kanno
 
PPTX
Introduction to NOSQL And MongoDB
Behrouz Bakhtiari
 
PPTX
Unit 1 NoSQL commands.pptx
RanjithaM32
 
PDF
Mongodb mock test_ii
Ankit Dubey
 
PDF
Symfony2 and MongoDB - MidwestPHP 2013
Pablo Godel
 
MongoDB For C++ Developers
Ynon Perek
 
Introduction To MongoDB
Ynon Perek
 
Mongo db basics
Claudio Montoya
 
MongoSV Schema Workshop
MongoDB
 
Building Apps with MongoDB
Nate Abele
 
MongoDB @ Frankfurt NoSql User Group
Chris Harris
 
De normalised london aggregation framework overview
Chris Harris
 
Mongodb introduction and_internal(simple)
Kai Zhao
 
Schema Design
MongoDB
 
Building a MongoDB App with Perl
Mike Friedman
 
Introduction to MongoDB
S.Shayan Daneshvar
 
Mongo DB Presentation
Jaya Naresh Kovela
 
Mongo db
Raghu nath
 
Mongo db php_shaken_not_stirred_joomlafrappe
Spyros Passas
 
Mongo db basics
Harischandra M K
 
Mongo db
Toki Kanno
 
Introduction to NOSQL And MongoDB
Behrouz Bakhtiari
 
Unit 1 NoSQL commands.pptx
RanjithaM32
 
Mongodb mock test_ii
Ankit Dubey
 
Symfony2 and MongoDB - MidwestPHP 2013
Pablo Godel
 

More from Ynon Perek (20)

PDF
Regexp
Ynon Perek
 
PDF
Html5 intro
Ynon Perek
 
PDF
09 performance
Ynon Perek
 
PDF
Mobile Web Intro
Ynon Perek
 
PDF
Qt multi threads
Ynon Perek
 
PDF
Vimperl
Ynon Perek
 
PDF
Syllabus
Ynon Perek
 
PDF
Mobile Devices
Ynon Perek
 
PDF
Network
Ynon Perek
 
PDF
Architecture app
Ynon Perek
 
PDF
Cryptography
Ynon Perek
 
PDF
Unit Testing JavaScript Applications
Ynon Perek
 
PDF
How to write easy-to-test JavaScript
Ynon Perek
 
PDF
Introduction to Selenium and Ruby
Ynon Perek
 
PDF
Introduction To Web Application Testing
Ynon Perek
 
PDF
Accessibility
Ynon Perek
 
PDF
Angularjs
Ynon Perek
 
PDF
Js memory
Ynon Perek
 
PDF
Qt Design Patterns
Ynon Perek
 
PDF
Web Application Security
Ynon Perek
 
Regexp
Ynon Perek
 
Html5 intro
Ynon Perek
 
09 performance
Ynon Perek
 
Mobile Web Intro
Ynon Perek
 
Qt multi threads
Ynon Perek
 
Vimperl
Ynon Perek
 
Syllabus
Ynon Perek
 
Mobile Devices
Ynon Perek
 
Network
Ynon Perek
 
Architecture app
Ynon Perek
 
Cryptography
Ynon Perek
 
Unit Testing JavaScript Applications
Ynon Perek
 
How to write easy-to-test JavaScript
Ynon Perek
 
Introduction to Selenium and Ruby
Ynon Perek
 
Introduction To Web Application Testing
Ynon Perek
 
Accessibility
Ynon Perek
 
Angularjs
Ynon Perek
 
Js memory
Ynon Perek
 
Qt Design Patterns
Ynon Perek
 
Web Application Security
Ynon Perek
 
Ad

Recently uploaded (20)

PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Ad

MongoDB for Perl Developers

  • 1. MongoDB For Perl Developers { author: “Ynon Perek” } Friday, March 1, 13
  • 2. Whoami Ynon Perek http://ynonperek.com [email protected] Friday, March 1, 13
  • 3. Agenda Mongo Is Awesome CRUD Operations The Driver Coding Time Friday, March 1, 13
  • 4. Mongo Is Awesome Data Store for JSON Objects Friday, March 1, 13
  • 5. Mongo Is Awesome Data Store for JSON Objects { “Name” : “Rose Tyler” } Friday, March 1, 13
  • 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 more Friday, March 1, 13
  • 7. It’s A Document Oriented Data Store Friday, March 1, 13
  • 8. It don’t do joins Friday, March 1, 13
  • 9. It don’t do transactions Friday, March 1, 13
  • 10. Keeping It Simple Document Oriented No Transactions No Joins Friday, March 1, 13
  • 11. Application Architecture APP DB Friday, March 1, 13
  • 12. What Can Mongo Do For You Create and store objects Arrange them in collections Retrieve them later Friday, March 1, 13
  • 14. CRUD Operations Create, Read, Update and Destroy Data Friday, March 1, 13
  • 15. Mongo CRUD Create is called insert Read is called find Update is called update Destroy is called remove Friday, March 1, 13
  • 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. 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. 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
  • 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. Query Document An empty (or missing) query document returns everything db.collection.find({}) db.collection.find() Friday, March 1, 13
  • 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. 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. 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. Quiz: What Is Returned from alterego publisher Bruce { Earth DC Wayne “publisher” : “DC” } Peter Earth Marvel Parker Krypton Clark Kent DC Friday, March 1, 13
  • 26. Quiz: What Is Returned from alterego publisher { Bruce “publisher” : Earth DC Wayne “DC”, “from” : “Earth” Peter Earth Marvel } Parker Krypton Clark Kent DC Friday, March 1, 13
  • 27. Resources Queries Cheat Sheet http://www.10gen.com/sites/default/files/downloads/ mongodb_qrc_queries.pdf Friday, March 1, 13
  • 29. Update The general form for update is: db.collection.update( <query>, <update>, <options> ) Which Entries What to do with to update them Friday, March 1, 13
  • 30. Update Some Update Operators “$set”, “$inc” “$push”, “$pushAll”, “$addToSet” “$pop”, “$pull”, “$pullAll” Friday, March 1, 13
  • 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. 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. Deleting Data db.posts.remove( { “author” : “Father Angelo” }) db.music.remove({ “genres” : “pop” }) db.posts.remove({ “tags” : “funny” }, 1); Friday, March 1, 13
  • 35. The Driver Hello MongoDB Connecting and Authenticating Querying/Updating Data Coding Time Friday, March 1, 13
  • 36. Hello MongoDB Mike Friedman, Kristina Chodorow and rafl MongoDB::Examples MongoDB::Tutorial Alternative Driver: Mango Friday, March 1, 13
  • 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. 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. Querying Data my $cursor = $coll->find({name => 'Tom'});   while ( my $next = $cursor->next ) {   say $next->{name}, " Got: ", $next->{score}; } Friday, March 1, 13
  • 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. Update Data $coll->update( { name => 'Tom' }, { '$inc' => { score => 1 } } ); $coll->update( { name => 'Tom' }, { '$inc' => { score => 1 } }, { multiple => 1 } ); Friday, March 1, 13
  • 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. Coding Time Mongo Log Parsing Mongo Web Address Book Code Examples At: https://github.com/ ynonp/perl- workshop-2013 Friday, March 1, 13
  • 44. Thanks For Listening Ynon Perek Slides at: ynonperek.com Talk to me at: [email protected] Friday, March 1, 13