48
Perl Oasis 2013 Building Your App: Perl & MongoDB Mike Friedman (friedo) Perl Engineer & Evangelist, 10gen http://friedo.com Thursday, January 17, 13

Building a MongoDB App with Perl

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 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

Page 2: Building a MongoDB App with Perl

10gen, the MongoDB Company

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

Thursday, January 17, 13

Page 3: Building a MongoDB App with Perl

What is MongoDB?

Thursday, January 17, 13

Page 4: Building a MongoDB App with Perl

What is MongoDB?

✤ Document Oriented Database

Thursday, January 17, 13

Page 5: Building a MongoDB App with Perl

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

Thursday, January 17, 13

Page 6: Building a MongoDB App with Perl

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

✤ High Performance

Thursday, January 17, 13

Page 7: Building a MongoDB App with Perl

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

✤ High Performance

✤ Horizontally Scalable

Thursday, January 17, 13

Page 8: Building a MongoDB App with Perl

What is MongoDB?

✤ Document Oriented Database

✤ Open Source

✤ High Performance

✤ Horizontally Scalable

✤ Full Featured

Thursday, January 17, 13

Page 9: Building a MongoDB App with Perl

What We Will Build:

✤ “Library” - A demo application

✤ Users

✤ Books

✤ Authors

✤ Publishers

Thursday, January 17, 13

Page 10: Building a MongoDB App with Perl

What We Will Need:

Thursday, January 17, 13

Page 11: Building a MongoDB App with Perl

What We Will Need:

✤ MongoDB

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

Thursday, January 17, 13

Page 12: Building a MongoDB App with Perl

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

Page 13: Building a MongoDB App with Perl

Inside the MongoDB CPAN Distribution

Thursday, January 17, 13

Page 14: Building a MongoDB App with Perl

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

Thursday, January 17, 13

Page 15: Building a MongoDB App with Perl

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

✤ new in version 0.502.

Thursday, January 17, 13

Page 16: Building a MongoDB App with Perl

Inside the MongoDB CPAN Distribution

✤ MongoDB::MongoClient

✤ new in version 0.502.

✤ “Safe” (write-acknowledged) by default

Thursday, January 17, 13

Page 17: Building a MongoDB App with Perl

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

Page 18: Building a MongoDB App with Perl

Inside the MongoDB CPAN Distribution

Thursday, January 17, 13

Page 19: Building a MongoDB App with Perl

Inside the MongoDB CPAN Distribution

✤ MongoDB::Database

✤ Represents a database (namespace) on the MongoDB server

Thursday, January 17, 13

Page 20: Building a MongoDB App with Perl

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

Page 21: Building a MongoDB App with Perl

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

Page 22: Building a MongoDB App with Perl

MongoDB Documents

Thursday, January 17, 13

Page 23: Building a MongoDB App with Perl

MongoDB Documents

✤ Documents live in Collections

Thursday, January 17, 13

Page 24: Building a MongoDB App with Perl

MongoDB Documents

✤ Documents live in Collections

✤ Documents have no pre-defined schema

Thursday, January 17, 13

Page 25: Building a MongoDB App with Perl

MongoDB Documents

✤ Documents live in Collections

✤ Documents have no pre-defined schema

✤ Documents have key-value pairs, like Perl hashes

Thursday, January 17, 13

Page 26: Building a MongoDB App with Perl

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

Page 27: Building a MongoDB App with Perl

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

Page 28: Building a MongoDB App with Perl

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

Page 29: Building a MongoDB App with Perl

Class Delegation Structure

Documents

Thursday, January 17, 13

Page 30: Building a MongoDB App with Perl

Class Delegation Structure

MongoDB::CursorDocuments

Thursday, January 17, 13

Page 31: Building a MongoDB App with Perl

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

Thursday, January 17, 13

Page 32: Building a MongoDB App with Perl

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

has()

MongoDB::Database

Thursday, January 17, 13

Page 33: Building a MongoDB App with Perl

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

has()

MongoDB::Database

has()

MongoDB::MongoClient

Thursday, January 17, 13

Page 34: Building a MongoDB App with Perl

Class Delegation Structure

MongoDB::CursorDocuments

has()

MongoDB::Collection

has()

MongoDB::Database

has()

MongoDB::MongoClient

Application

Thursday, January 17, 13

Page 35: Building a MongoDB App with Perl

Let’s Build It!

Thursday, January 17, 13

Page 36: Building a MongoDB App with Perl

Building the Library

Thursday, January 17, 13

Page 37: Building a MongoDB App with Perl

Building the Library

Thursday, January 17, 13

Page 38: Building a MongoDB App with Perl

Building the Library

Thursday, January 17, 13

Page 39: Building a MongoDB App with Perl

Building the Library

Thursday, January 17, 13

Page 40: Building a MongoDB App with Perl

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

Page 41: Building a MongoDB App with Perl

Building the Library

Thursday, January 17, 13

Page 42: Building a MongoDB App with Perl

Building the Library

Thursday, January 17, 13

Page 43: Building a MongoDB App with Perl

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

Page 44: Building a MongoDB App with Perl

What’s on CPAN?

✤ ODM’s (Object Document Mappers)

✤ Like ORMs but simpler

Thursday, January 17, 13

Page 45: Building a MongoDB App with Perl

What’s on CPAN?

✤ Mongoose:

✤ Based on MongoMapperfrom Ruby.

✤ MongoDB Docs --> Moose objects.

Thursday, January 17, 13

Page 46: Building a MongoDB App with Perl

What’s on CPAN?

✤ MongoDBI

✤ Very Perlish

✤ Moose-like Syntax

Thursday, January 17, 13

Page 47: Building a MongoDB App with Perl

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

Page 48: Building a MongoDB App with Perl

Questions

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

Thursday, January 17, 13