mongodb for perl developers
TRANSCRIPT
MongoDB For Perl Developers{ author: “Ynon Perek” }
Friday, March 1, 13
Whoami
Ynon Perek
http://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
Keys are simple strings
Values can be: Numbers, Strings, Arrays, Other Objects, and more
{ "name" : "Rose Tyler", "race" : "Human", "body parts" : [ "head", "legs"]}
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
DBAPP
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 OperationsCreate, 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
find and findOne perform read operations
Both take a query
find returns a cursor
findOne returns an object
db.collection.find( <query>, <projection> )
Reading Data
Optional: Fields to fetch
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
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 }});
Query Object
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
{ “publisher” : “DC”}
from alterego publisher
Earth Bruce Wayne DC
Earth Peter Parker Marvel
Krypton Clark Kent DC
Friday, March 1, 13
Quiz: What Is Returned
{ “publisher” : “DC”, “from” : “Earth”}
from alterego publisher
Earth Bruce Wayne DC
Earth Peter Parker Marvel
Krypton Clark Kent DC
Friday, March 1, 13
Resources
Queries Cheat Sheethttp://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 to update
What to do with 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: [email protected]
Friday, March 1, 13