intro to mongodb - donutsdocshare01.docshare.tips/files/28221/282218088.pdf · intro to mongodb...

66
Intro to MongoDB Alex Sharp twitter : @ajsharp email : [email protected] Monday, February 22, 2010

Upload: others

Post on 21-Jun-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Intro to MongoDBAlex Sharp

twitter: @ajsharp

email: [email protected]

Monday, February 22, 2010

Page 2: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

So what is MongoDB?

Monday, February 22, 2010

Page 3: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

First and foremost...

Monday, February 22, 2010

Page 4: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

IT’S THE NEW HOTNESS!!!

Monday, February 22, 2010

Page 5: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

omgomgomg SHINY OBJECTS

omgomgomg

Monday, February 22, 2010

Page 6: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

MongoDB (from "humongous") is a scalable, high-performance, open source,

schema-free, document-oriented database.

- mongodb.org

Monday, February 22, 2010

Page 7: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

Monday, February 22, 2010

Page 8: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

“One size fits all” approach no longer applies

Monday, February 22, 2010

Page 9: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

Non-relational DBs scale more easily, especially horizontally

Monday, February 22, 2010

Page 10: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

Focus on speed, performance, flexibility and scalability

Monday, February 22, 2010

Page 11: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

Not concerned with transactional stuff and relational semantics

Monday, February 22, 2010

Page 12: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

DBs should be an on-demand commodity, in a cloud-like fashion

Monday, February 22, 2010

Page 13: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Philosophy

Mongo tries to achieve the performance of traditional key-value stores while maintaining functionality of traditional RDBMS

Monday, February 22, 2010

Page 14: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Monday, February 22, 2010

Page 15: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Standard database stuff

Monday, February 22, 2010

Page 16: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Standard database stuff

Indexing

Monday, February 22, 2010

Page 17: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Standard database stuff

Indexing

replication/failover support

Monday, February 22, 2010

Page 18: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features: Document Storage

Documents are stored in BSON (binary JSON)

Monday, February 22, 2010

Page 19: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

BSON is a binary serialization of JSON-like objects

Features: Document Storage

Monday, February 22, 2010

Page 20: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

This is extremely powerful, b/c it means mongo understands JSON natively

Features: Document Storage

Monday, February 22, 2010

Page 21: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Any valid JSON can be easily imported and queried

Features: Document Storage

Monday, February 22, 2010

Page 22: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Schema-less; very flexible

Monday, February 22, 2010

Page 23: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Schema-less; very flexibleno more blocking ALTER TABLE

Monday, February 22, 2010

Page 24: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Auto-sharding (alpha)

Monday, February 22, 2010

Page 25: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Makes for easy horizontal scaling

Monday, February 22, 2010

Page 26: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Map/Reduce

Monday, February 22, 2010

Page 27: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Very, very fast

Monday, February 22, 2010

Page 28: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Super easy to install

Monday, February 22, 2010

Page 29: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Strong with major languages

Monday, February 22, 2010

Page 30: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features

Document-oriented = flexible

Monday, February 22, 2010

Page 31: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features: Querying

Rich, javascript-based query syntax

Monday, February 22, 2010

Page 32: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features: Querying

Rich, javascript-based query syntax

Allows us to deep, nested queries

Monday, February 22, 2010

Page 33: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features: Querying

Rich, javascript-based query syntax

Allows us to do deep, nested queries

db.order.find( { shipping: { carrier: "usps" } } );

Monday, February 22, 2010

Page 34: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features: Querying

Rich, javascript-based query syntax

Allows us to deep, nested queries

db.order.find( { shipping: { carrier: "usps" } } );

shipping is an embedded document (object)

Monday, February 22, 2010

Page 35: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Features: Binary Object Store

Efficient binary large object store via GridFS

Monday, February 22, 2010

Page 36: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Efficient binary large object store via GridFS

i.e. store images, videos, anything

Features: Binary Object Store

Monday, February 22, 2010

Page 37: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts

Monday, February 22, 2010

Page 38: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Document-oriented

Think of “documents” as database records

Monday, February 22, 2010

Page 39: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Document-oriented

Think of “documents” as database records

Documents are basically just JSON objects that Mongo stores in binary

Monday, February 22, 2010

Page 40: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Document-oriented

Think of “collections” as database tables

Monday, February 22, 2010

Page 41: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

RDBMS (mysql, postgres) MongoDB

Tables Collections

Concept Mapping

Monday, February 22, 2010

Page 42: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

RDBMS (mysql, postgres) MongoDB

Tables Collections

Records/rows Documents/objects

Concept Mapping

Monday, February 22, 2010

Page 43: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

RDBMS (mysql, postgres) MongoDB

Tables Collections

Records/rows Documents/objects

Queries return record(s) Queries return a cursor

Concept Mapping

Monday, February 22, 2010

Page 44: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

RDBMS (mysql, postgres) MongoDB

Tables Collections

Records/rows Documents/objects

Queries return record(s) Queries return a cursor

Concept Mapping

???Monday, February 22, 2010

Page 45: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Cursors

Queries return “cursors” instead of collections

Monday, February 22, 2010

Page 46: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Cursors

Queries return “cursors” instead of collections

A cursor allows you to iterate through the result set

Monday, February 22, 2010

Page 47: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Cursors

Queries return “cursors” instead of collections

A cursor allows you to iterate through the result set

A big reason for this is performance

Monday, February 22, 2010

Page 48: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Cursors

Queries return “cursors” instead of collections

A cursor allows you to iterate through the result set

A big reason for this is performance

Much more efficient than loading all objects into memory

Monday, February 22, 2010

Page 49: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Cursors

The find() function returns a cursor object

Monday, February 22, 2010

Page 50: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Concepts: Cursors

The find() function returns a cursor object

var cursor = db.logged_requests.find({ 'status_code' : 200 })

cursor.hasNext() // "true"

cursor.forEach( function(item) { print(tojson(item)) });

cursor.hasNext() // "false"

Monday, February 22, 2010

Page 51: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Features

Monday, February 22, 2010

Page 52: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Features

Capped collections

Monday, February 22, 2010

Page 53: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Features

Capped collections

Fixed-sized, limited operation, auto-LRU age-out collections

Monday, February 22, 2010

Page 54: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Features

Capped collections

Fixed-sized, limited operation, auto-LRU age-out collections

Fixed insertion order

Monday, February 22, 2010

Page 55: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Features

Capped collections

Fixed-sized, limited operation, auto-LRU age-out collections

Fixed insertion order

Super fast

Monday, February 22, 2010

Page 56: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Features

Capped collections

Fixed-sized, limited operation, auto-LRU age-out collections

Fixed insertion order

Super fast

Ideal for logging and caching

Monday, February 22, 2010

Page 57: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Data Warehouse

Mongo understands JSON natively

Monday, February 22, 2010

Page 58: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Data Warehouse

Mongo understands JSON natively

Very powerful for analysis

Monday, February 22, 2010

Page 59: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Data Warehouse

Mongo understands JSON natively

Very powerful for analysis

Query a bunch of data from some web service

Monday, February 22, 2010

Page 60: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Data Warehouse

Mongo understands JSON natively

Very powerful for analysis

Query a bunch of data from some web service

Import into mongo (mongoimport -f filename.json)

Monday, February 22, 2010

Page 61: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Data Warehouse

Mongo understands JSON natively

Very powerful for analysis

Query a bunch of data from some web service

Import into mongo (mongoimport -f filename.json)

Analyze to your heart’s content

Monday, February 22, 2010

Page 62: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Harmonyapp.com

Large rails app for building websites (kind of a CMS)

Monday, February 22, 2010

Page 63: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Cool Uses

Hardcore debugging

Spit out large amounts of data

Monday, February 22, 2010

Page 64: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Limitations

Transaction support

Monday, February 22, 2010

Page 65: Intro to MongoDB - Donutsdocshare01.docshare.tips/files/28221/282218088.pdf · Intro to MongoDB Alex Sharp twitter: @ajsharp email: ajsharp@frothlogic.com Monday, February 22, 2010

Limitations

Transaction support

Relational integrity

Monday, February 22, 2010