mongodb mojo: building a basic perl app

27
Stephen Steneker (@stennie) #MongoDB MongoDB Mojo: Building a basic Perl app Community Support Manager, MongoDB

Post on 18-Oct-2014

1.121 views

Category:

Technology


0 download

DESCRIPTION

Building a basic app using MongoDB + Perl. From Sydney Perl Mongers meetup (September, 2013).

TRANSCRIPT

Page 1: MongoDB Mojo: Building a Basic Perl App

Stephen Steneker (@stennie)

#MongoDB

MongoDB Mojo: Building a basic Perl app

Community Support Manager, MongoDB

Page 2: MongoDB Mojo: Building a Basic Perl App

MongoDB is a ___________ database

•  Document oriented

•  Open source

•  High performance

•  Horizontally scalable

•  Full featured

Page 3: MongoDB Mojo: Building a Basic Perl App

High Performance

•  Written in C++

•  Extensive use of memory-mapped files i.e. read-through write-through memory caching.

•  Runs nearly everywhere

•  Data serialized as BSON (fast parsing)

•  Full support for primary & secondary indexes

•  Document model = less work

Page 4: MongoDB Mojo: Building a Basic Perl App

Depth of Functionality

Scal

abili

ty &

Per

form

ance

Memcached

MongoDB

RDBMS

Database Landscape

Page 5: MongoDB Mojo: Building a Basic Perl App

Full Featured

•  Ad Hoc queries

•  Real time aggregation

•  Rich query capabilities

•  Strongly consistent

•  Geospatial features

•  Support for most programming languages

•  Flexible schema

Page 6: MongoDB Mojo: Building a Basic Perl App

What We Will Build:

•  “Library”: A demo application –  Users –  Books –  Authors –  Publishers

Photo by Jessica Spengler: http://www.flickr.com/photos/wordridden/6798197508/

Page 7: MongoDB Mojo: Building a Basic Perl App

What We Will Need:

•  MongoDB –  http://www.mongodb.org/downloads

•  CPAN Modules: –  https://metacpan.org/module/Mojolicious::Lite –  https://metacpan.org/module/MongoDB

Page 8: MongoDB Mojo: Building a Basic Perl App

Inside the CPAN Distribution: Requirements

•  Moose

•  DateTime

•  Digest::MD5

•  Tie::IxHash

•  XSLoader

•  boolean

Page 9: MongoDB Mojo: Building a Basic Perl App

Inside the CPAN Distribution: Client

•  MongoDB::MongoClient –  new in version 0.502 (Nov-2012) –  “Safe” (write-acknowledged) by default –  Encapsulates connection and server info

Page 10: MongoDB Mojo: Building a Basic Perl App

Inside the MongoDB CPAN Distribution: Classes

•  MongoDB::Database –  Represents a database (namespace) on the MongoDB server

•  MongoDB::Collection –  Represents a collection (table? kinda) in a database

•  MongoDB::Cursor –  Retrieves documents (rows? kinda) from a collection

Page 11: MongoDB Mojo: Building a Basic Perl App

MongoDB Documents

•  Documents live in Collections

•  Documents have no pre-defined schema

•  Documents have key-value pairs, like Perl hashes

•  Documents can have nested structure (arrays and other documents), like Perl hashes

•  Documents look something like JSON

Page 12: MongoDB Mojo: Building a Basic Perl App

MongoDB Documents

{ 'title': 'Fellowship of the Ring, The', 'author': ObjectId("507ffbb1d94ccab2da652597"), 'language': 'English', 'genre': ['fantasy', 'adventure'], 'publication': { 'name': 'George Allen & Unwin', 'location': 'London',

'date': new Date('21 July 1954'), }

}

Page 13: MongoDB Mojo: Building a Basic Perl App

Class Delegation Structure

MongoDB::Cursor Documents

has()!

MongoDB::Collection

has()!

MongoDB::Database

has()!

MongoDB::MongoClient

Application

Page 14: MongoDB Mojo: Building a Basic Perl App

Let’s Build It!

Page 15: MongoDB Mojo: Building a Basic Perl App

Building the Library

Page 16: MongoDB Mojo: Building a Basic Perl App

Building the Library

Page 17: MongoDB Mojo: Building a Basic Perl App

Building the Library

Page 18: MongoDB Mojo: Building a Basic Perl App

Building the Library

Page 19: MongoDB Mojo: Building a Basic Perl App

Building the Library

Remember the Genres? {

'title': 'Fellowship of the Ring, The', 'author': ObjectId("507ffbb1d94ccab2da652597"), 'language': 'English', 'genre': ['fantasy', 'adventure'], 'publication': { 'name': 'George Allen & Unwin', 'location': 'London',

'date': new Date('21 July 1954'), }

}

Page 20: MongoDB Mojo: Building a Basic Perl App

Building the Library

Page 21: MongoDB Mojo: Building a Basic Perl App

Building the Library

Page 22: MongoDB Mojo: Building a Basic Perl App

What’s on CPAN?

•  ODMs (Object Document Mappers) –  Like ORMs but simpler

Page 23: MongoDB Mojo: Building a Basic Perl App

Mongoose

•  Based on MongoMapper from Ruby.

•  MongoDB Docs --> Moose objects.

http://search.cpan.org/~rodrigo/Mongoose-0.23/ (28-Jan-2013)

Page 24: MongoDB Mojo: Building a Basic Perl App

MongoDBI

•  Very Perlish

•  Moose-like Syntax

http://search.cpan.org/~awncorp/MongoDBI-0.02/ (17-Oct-2012)

Page 25: MongoDB Mojo: Building a Basic Perl App

MongoDB::Async

•  Tracks upstream MongoDB Distribution

•  Uses Coro and libev for asynchronous queries

•  (Mostly) drop-in replacement for MongoDB driver

http://search.cpan.org/~nyaknyan/MongoDB-Async-0.503.2/ (22-Dec-2012)

Page 26: MongoDB Mojo: Building a Basic Perl App

Where to from here? •  Learn more about MongoDB

–  http://docs.mongodb.org

•  Learn about the MongoDB Perl API –  https://metacpan.org/module/MongoDB::Tutorial

•  Community support –  http://stackoverflow.com/tags/mongodb –  https://groups.google.com/group/mongodb-user

•  MongoDB University –  http://education.mongodb.com/

Page 27: MongoDB Mojo: Building a Basic Perl App

[email protected]

Stephen Steneker (@stennie)

#MongoDB

Thank You!