44
MongoDB For Perl Developers { author: “Ynon Perek” } Friday, March 1, 13

MongoDB for Perl Developers

Embed Size (px)

Citation preview

Page 1: MongoDB for Perl Developers

MongoDB For Perl Developers{ author: “Ynon Perek” }

Friday, March 1, 13

Page 3: MongoDB for Perl Developers

Agenda

Mongo Is Awesome

CRUD Operations

The Driver

Coding Time

Friday, March 1, 13

Page 4: MongoDB for Perl Developers

Mongo Is Awesome

Data Store for JSON Objects

Friday, March 1, 13

Page 5: MongoDB for Perl Developers

Mongo Is Awesome

Data Store for JSON Objects

{ “Name” : “Rose Tyler” }

Friday, March 1, 13

Page 6: MongoDB for Perl Developers

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

Page 7: MongoDB for Perl Developers

It’s A Document Oriented Data Store

Friday, March 1, 13

Page 8: MongoDB for Perl Developers

It don’t do joins

Friday, March 1, 13

Page 9: MongoDB for Perl Developers

It don’t do transactions

Friday, March 1, 13

Page 10: MongoDB for Perl Developers

Keeping It Simple

Document Oriented

No Transactions

No Joins

Friday, March 1, 13

Page 11: MongoDB for Perl Developers

Application Architecture

DBAPP

Friday, March 1, 13

Page 12: MongoDB for Perl Developers

What Can Mongo Do For You

Create and store objects

Arrange them in collections

Retrieve them later

Friday, March 1, 13

Page 13: MongoDB for Perl Developers

Q & A

Friday, March 1, 13

Page 14: MongoDB for Perl Developers

CRUD OperationsCreate, Read, Update and Destroy Data

Friday, March 1, 13

Page 15: MongoDB for Perl Developers

Mongo CRUD

Create is called insert

Read is called find

Update is called update

Destroy is called remove

Friday, March 1, 13

Page 16: MongoDB for Perl Developers

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

Page 17: MongoDB for Perl Developers

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

Page 18: MongoDB for Perl Developers

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

Page 19: MongoDB for Perl Developers

Demo: Insert

Friday, March 1, 13

Page 20: MongoDB for Perl Developers

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

Page 21: MongoDB for Perl Developers

Query Document

An empty (or missing) query document returns everything

db.collection.find({})

db.collection.find()

Friday, March 1, 13

Page 22: MongoDB for Perl Developers

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

Page 23: MongoDB for Perl Developers

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

Page 24: MongoDB for Perl Developers

Query Document

A compound query means a logical AND on the conditions.

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

Friday, March 1, 13

Page 25: MongoDB for Perl Developers

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

Page 26: MongoDB for Perl Developers

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

Page 28: MongoDB for Perl Developers

Demo: Query

Friday, March 1, 13

Page 29: MongoDB for Perl Developers

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

Page 30: MongoDB for Perl Developers

Update

Some Update Operators

“$set”, “$inc”

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

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

Friday, March 1, 13

Page 31: MongoDB for Perl Developers

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

Page 32: MongoDB for Perl Developers

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

Page 33: MongoDB for Perl Developers

Deleting Data

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

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

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

Friday, March 1, 13

Page 34: MongoDB for Perl Developers

Q & A

Friday, March 1, 13

Page 35: MongoDB for Perl Developers

The Driver

Hello MongoDB

Connecting and Authenticating

Querying/Updating Data

Coding Time

Friday, March 1, 13

Page 36: MongoDB for Perl Developers

Hello MongoDB

Mike Friedman, Kristina Chodorow and rafl

MongoDB::Examples

MongoDB::Tutorial

Alternative Driver: Mango

Friday, March 1, 13

Page 37: MongoDB for Perl Developers

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

Page 38: MongoDB for Perl Developers

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

Page 39: MongoDB for Perl Developers

Querying Data

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

Friday, March 1, 13

Page 40: MongoDB for Perl Developers

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

Page 41: MongoDB for Perl Developers

Update Data

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

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

Friday, March 1, 13

Page 42: MongoDB for Perl Developers

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

Page 44: MongoDB for Perl Developers

Thanks For Listening

Ynon Perek

Slides at:ynonperek.com

Talk to me at: [email protected]

Friday, March 1, 13