Building a MongoDB App with Perl

Post on 10-May-2015

3.283 views 4 download

Tags:

description

This presentation covers the very basics of building a simple web application with MongoDB and the Mojolicious::Lite prototyping framework.

Transcript of Building a MongoDB App with Perl

Perl Oasis 2013

Building Your App: Perl & MongoDBMike Friedman (friedo)Perl Engineer & Evangelist, 10genhttp://friedo.com

Thursday, January 17, 13

10gen, the MongoDB Company

http://www.10gen.com http://www.mongodb.org

Thursday, January 17, 13

What is MongoDB?

Thursday, January 17, 13

What is MongoDB?

✤ Document Oriented Database

Thursday, January 17, 13

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

Thursday, January 17, 13

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

✤ High Performance

Thursday, January 17, 13

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

✤ High Performance

✤ Horizontally Scalable

Thursday, January 17, 13

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

✤ High Performance

✤ Horizontally Scalable

✤ Full Featured

Thursday, January 17, 13

What We Will Build:

✤ “Library” - A demo application

✤ Users

✤ Books

✤ Authors

✤ Publishers

Thursday, January 17, 13

What We Will Need:

Thursday, January 17, 13

What We Will Need:

✤ MongoDB

✤ http://www.mongodb.org/downloads

Thursday, January 17, 13

What We Will Need:

✤ MongoDB

✤ http://www.mongodb.org/downloads

✤ CPAN Modules:

✤ https://metacpan.org/module/Mojolicious::Lite

✤ https://metacpan.org/module/MongoDB

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

✤ new in version 0.502.

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

✤ new in version 0.502.

✤ “Safe” (write-acknowledged) by default

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

✤ new in version 0.502.

✤ “Safe” (write-acknowledged) by default

✤ Encapsulates connection and server info

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ MongoDB::Database

✤ Represents a database (namespace) on the MongoDB server

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ MongoDB::Database

✤ Represents a database (namespace) on the MongoDB server

✤ MongoDB::Collection

✤ Represents a collection (table? kinda) in a database

Thursday, January 17, 13

Inside the MongoDB CPAN Distribution

✤ 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

Thursday, January 17, 13

MongoDB Documents

Thursday, January 17, 13

MongoDB Documents

✤ Documents live in Collections

Thursday, January 17, 13

MongoDB Documents

✤ Documents live in Collections

✤ Documents have no pre-defined schema

Thursday, January 17, 13

MongoDB Documents

✤ Documents live in Collections

✤ Documents have no pre-defined schema

✤ Documents have key-value pairs, like Perl hashes

Thursday, January 17, 13

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

Thursday, January 17, 13

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

Thursday, January 17, 13

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'), }

}

Thursday, January 17, 13

Class Delegation Structure

Documents

Thursday, January 17, 13

Class Delegation Structure

MongoDB::CursorDocuments

Thursday, January 17, 13

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

Thursday, January 17, 13

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

has()

MongoDB::Database

Thursday, January 17, 13

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

has()

MongoDB::Database

has()

MongoDB::MongoClient

Thursday, January 17, 13

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

has()

MongoDB::Database

has()

MongoDB::MongoClient

Application

Thursday, January 17, 13

Let’s Build It!

Thursday, January 17, 13

Building the Library

Thursday, January 17, 13

Building the Library

Thursday, January 17, 13

Building the Library

Thursday, January 17, 13

Building the Library

Thursday, January 17, 13

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'), }}

Thursday, January 17, 13

Building the Library

Thursday, January 17, 13

Building the Library

Thursday, January 17, 13

Where to go from here?

✤ Learn more about MongoDB:✤ http://docs.mongodb.org/manual/

✤ Learn more about the MongoDB Perl API✤ https://metacpan.org/module/MongoDB::Tutorial

✤ Hack on the Library demo app✤ https://github.com/friedo/mongo-library✤ Add/edit authors?✤ Edit books?

Thursday, January 17, 13

What’s on CPAN?

✤ ODM’s (Object Document Mappers)

✤ Like ORMs but simpler

Thursday, January 17, 13

What’s on CPAN?

✤ Mongoose:

✤ Based on MongoMapperfrom Ruby.

✤ MongoDB Docs --> Moose objects.

Thursday, January 17, 13

What’s on CPAN?

✤ MongoDBI

✤ Very Perlish

✤ Moose-like Syntax

Thursday, January 17, 13

What’s on CPAN?

✤ MongoDB::Async

✤ Tracks upstream MongoDB Distribution

✤ Uses Coro and libev for asynchronous queries

✤ (Mostly) drop-in replacement for MongoDB driver

Thursday, January 17, 13

Questions

https://github.com/friedo/mongo-libraryhttp://docs.mongodb.org/manual/MongoDB::Tutorial

Thursday, January 17, 13