Top Banner
Introduction to
127

Introduction To MongoDB

Nov 17, 2014

Download

Documents

kristina8955

FOSDEM NoSQL devroom talk, February 7, 2010.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction To MongoDB

Introduction to

Page 2: Introduction To MongoDB

"MongoDB is a scalable, high-performance, open source, schema-free, document-oriented database."

Page 3: Introduction To MongoDB

Goal: be the best database for (most) web apps

...not the best DB for everything.

Page 4: Introduction To MongoDB

What do Web Apps Want?

Performance

Scalability

Availability

Page 5: Introduction To MongoDB

What do developers want?

Page 6: Introduction To MongoDB
Page 7: Introduction To MongoDB

What else do developers want?

Easier development

Less maintenance (at 3am)

Great support

Page 8: Introduction To MongoDB

Goals Easy-to-use

Fast

Always available

Easy to scale

Page 9: Introduction To MongoDB

Goals Easy-to-use

Fast

Always available

Easy to scale

Page 10: Introduction To MongoDB

5-minute install

Download binaries from

http://www.mongodb.org

and run!

Page 11: Introduction To MongoDB

Mongo knows JavaScript

$ ./mongo

MongoDB shell version: 1.3.2

url: test

connecting to: test

type "help" for help

>

Page 12: Introduction To MongoDB

Mongo knows JavaScript

$ ./mongo

MongoDB shell version: 1.3.2

url: test

connecting to: test

type "help" for help

> x = 123+1

124

>

Page 13: Introduction To MongoDB
Page 14: Introduction To MongoDB

mongo.kylebanker.com

Page 15: Introduction To MongoDB

Basic Usage

$ ./mongo

MongoDB shell version: 1.3.2

url: test

connecting to: test

type "help" for help

>

Page 16: Introduction To MongoDB

Basic Usage

$ ./mongo

MongoDB shell version: 1.3.2

url: test

connecting to: test

type "help" for help

> db

test

>

Page 17: Introduction To MongoDB

Database ≈ Database> use foo

switched to db foo

>

Page 18: Introduction To MongoDB

Database ≈ Database> use foo

switched to db foo

> db

foo

>

Page 19: Introduction To MongoDB

Collection ≈ Table> db.my_collection

foo.my_collection

>

Page 20: Introduction To MongoDB

Collection ≈ Table> db.my_collection

foo.my_collection

>

Page 21: Introduction To MongoDB

Collection ≈ Table> db.my_collection

foo.my_collection

> db.jzxgtx.find()

>

Page 22: Introduction To MongoDB

Collection ≈ Table> db.my_collection

foo.my_collection

> db.jzxgtx.find()

> db.pltzbt.count()

0

>

Page 23: Introduction To MongoDB

Document ≈ Row{

"title" : "My first blog post",

"author" : "Fred",

"content" : "Hello, world!",

"comments" : []

}

Page 24: Introduction To MongoDB

Documents

Language Document

JavaScript {"foo" : "bar"}

Perl {"foo" => "bar"}

PHP array("foo" => "bar")

Python {"foo" : "bar"}

Ruby {"foo" => "bar"}

Java DBObject obj = new BasicDBObject("foo", "bar");

Page 25: Introduction To MongoDB

Documents{

"title" : "My first blog post",

"author" : "Fred",

"content" : "Hello, world!",

"comments" : []

}

Page 26: Introduction To MongoDB

Documents> db.posts.insert({

... "title" : "My first blog post",

... "author" : "Fred",

... "content" : "Hello, world!",

... "comments" : [] })

>

Page 27: Introduction To MongoDB

Documents> post = db.posts.findOne()

{

"_id" : ObjectId("2fe3e4d892aa73234c910bed"),

"title" : "My first blog post",

"author" : "Fred",

"content" : "Hello, world!",

"comments" : []

}

>

Page 28: Introduction To MongoDB

Documents> post = db.posts.findOne()

{

"_id" : ObjectId("2fe3e4d892aa73234c910bed"),

"title" : "My first blog post",

"author" : "Fred",

"content" : "Hello, world!",

"comments" : []

}

>

Page 29: Introduction To MongoDB

ObjectIdQ: How do we make a unique ID quickly that is guaranteed to

be unique across multiple servers?

A:

> print(post._id)

2fe3e4d892aa73234c910bed

|------||----||--||----|

ts mac pid inc

Built-in document creation timestamp

Page 30: Introduction To MongoDB

A few examples...

Page 31: Introduction To MongoDB

Chess

Page 32: Introduction To MongoDB

{

"name" : "black king",

"symbol" : "♚",

"pos" : {

"x" : "e",

"y" : 8

}

}

Page 33: Introduction To MongoDB

Moving> db.chess.update(

{

"name" : "black king",

"symbol" : "♚",

"pos" : {"x":"e", "y":8}

}

Page 34: Introduction To MongoDB

Moving> db.chess.update(

... {"name" : "black king"},

{

"name" : "black king",

"symbol" : "♚",

"pos" : {"x":"e", "y":8}

}

Page 35: Introduction To MongoDB

Moving> db.chess.update(

... {"name" : "black king"},

... {"$inc" : {"pos.y" : -1}})

>

{

"name" : "black king",

"symbol" : "♚",

"pos" : {"x":"e", "y":8}

}

Page 36: Introduction To MongoDB

Moving> db.chess.update(

... {"name" : "black king"},

... {"$inc" : {"pos.y" : -1}})

>

Page 37: Introduction To MongoDB

Moving> db.chess.update(

... {"name" : "black king"},

... {"$inc" : {"pos.y" : -1}})

> db.chess.find({"name" : "black king"})

{

"name" : "black king",

"symbol" : "♚",

"pos" : {"x" : "e", "y" : 7}

}

Page 38: Introduction To MongoDB

Adding Information> db.chess.update(

... {"name" : /pawn/},

... {"$set" : {"importance" : 1}})

>

Page 39: Introduction To MongoDB

Adding Information> db.chess.update(

... {"name" : /pawn/},

... {"$set" : {"importance" : 1}})

> db.chess.findOne({"symbol" : "♙"})

{

"name" : "white pawn a",

"symbol" : "♙ ",

"pos" : {"x" : "a", "y" : 2},

"importance" : 1

}

Page 40: Introduction To MongoDB

Types null

boolean

integer

long

double

string

array

object

date

binary data

object id

regular expression

code

max value

min value

Page 41: Introduction To MongoDB

Querying> db.chess.find()

Page 42: Introduction To MongoDB

Querying

1

8

a h

Page 43: Introduction To MongoDB

Querying> db.chess.find().sort(

... {"pos.y" : -1, "pos.x" : 1})

Page 44: Introduction To MongoDB

Querying> db.chess.find(

... {"name" : /white/}).sort(

... {"pos.y" : -1, "pos.x" : 1})

Page 45: Introduction To MongoDB

Querying> db.chess.find().sort(

... {"importance" : -1}).limit(1)

Page 46: Introduction To MongoDB

Querying> db.chess.find().sort(

... {"importance" : -1}).limit(1).skip(1)

Page 47: Introduction To MongoDB

Query Conditions> db.chess.find({"pos.y" : 1})

Page 48: Introduction To MongoDB

Query Conditions> db.chess.find({"pos.y" :

... {"$gt" : 2, "$lt" : 7}})

Page 49: Introduction To MongoDB

Query Conditions> db.chess.find({"pos.y" :

... {"$gt" : 2, "$lt" : 7},

... "pos.x" :

... {"$ne" : "b"}})

Page 50: Introduction To MongoDB

"$gte"

Page 51: Introduction To MongoDB

"<null>"

Page 52: Introduction To MongoDB

""

Page 53: Introduction To MongoDB

'$gte' or "\$gte"

Page 54: Introduction To MongoDB

Or define your own!

":gte"

"=gte"

"?gte"

Page 55: Introduction To MongoDB

Or define your own!

" gte"

Page 56: Introduction To MongoDB

Or define your own!

" gte"

"♕ gte"

Page 57: Introduction To MongoDB

Or define your own!

" gte"

"♕ gte"

"xgte"

Page 58: Introduction To MongoDB

Other Neat Stuff

Page 59: Introduction To MongoDB

Analytics{

"uri" : "/blog",

"pageviews" : 0

}

Page 60: Introduction To MongoDB

Analytics - Increment Pageviewspage = db.analytics.findOne({"uri" : "/blog"});

if (page != null) {

page.pageviews++;

db.analytics.save(page);

}

else {

db.analytics.insert({

"uri" : "/blog",

"pageviews" : 1});

}

...that's 1 round trip + 1 update or insert.

Page 61: Introduction To MongoDB

Analytics - Upsert

db.analytics.update(

{ "uri" : "/blog" },

{ "$inc" : { "pageviews" => 1 } },

{ "upsert" : true });

...all in one update!

Page 62: Introduction To MongoDB

Capped Collections

4 GB, 50 MB, 97 documents, etc.

Page 63: Introduction To MongoDB

Capped Collections

4 GB, 50 MB, 97 documents, etc.

Page 64: Introduction To MongoDB

Capped Collections

4 GB, 50 MB, 97 documents, etc.

Page 65: Introduction To MongoDB

Storing Files - GridFS

Max: 4 MB

Page 66: Introduction To MongoDB

Storing Files - GridFS

(More than 4 MB)

Page 67: Introduction To MongoDB

Storing Files - GridFS

chunks

JJ J

J

JJ

J

J

J

J_id :files

Page 68: Introduction To MongoDB

Storing Files - GridFS

$grid = $db->get_gridfs();

$grid->insert($fh,

{"permissions" => 644,

"comment" => "vacation pics"});

Page 69: Introduction To MongoDB

Storing Files - GridFS

$file = $grid->find_one($query);

$file->print($another_fh);

Page 70: Introduction To MongoDB

JavaScript Functions> db.eval("function() { return 'hello'; }")

hello

>

Page 71: Introduction To MongoDB

JavaScript Functions> db.eval("function() { return 'hello'; }")

hello

>

> func = "function(x) {" +

... "return 'hello '+ x + '!';" +

... "}"

>

Page 72: Introduction To MongoDB

JavaScript Functions> db.eval("function() { return 'hello'; }")

hello

>

> func = "function(x) {" +

... "return 'hello '+ x + '!';" +

... "}"

> db.eval(func, ["joe"])

hello, joe!

Page 73: Introduction To MongoDB

"Stored Procedures"

> db.system.js.insert({

... "_id" : "x",

... "value" : 3});

>

> db.system.js.insert({

... "_id" : "y",

... "value" : 4});

>

Page 74: Introduction To MongoDB

"Stored Procedures"

> db.system.js.insert({

... "_id" : "x",

... "value" : 3});

>

> db.system.js.insert({

... "_id" : "y",

... "value" : 4});

>

> db.eval("return x+y");

7

Page 75: Introduction To MongoDB

Goals Easy-to-use

Fast

Always available

Easy to scale

Page 76: Introduction To MongoDB

Query Optimizer

CB

A

Page 77: Introduction To MongoDB

Query Optimizer

CB

A

Done!

Page 78: Introduction To MongoDB

Performance

"Any operation that takes longer than 0 milliseconds is suspect."

- Mongo user on IRC

Page 79: Introduction To MongoDB

Performance

Database Average Insert

MongoDB 0.00011

MySQL 0.00083

CouchDB 0.00640

Memcached 0.00279

100,000 inserts (with indexes for MongoDB and MySQL)

Page 80: Introduction To MongoDB

Performance

Database Average Query (Indexed)

MongoDB 0.00035

MySQL 0.00772

CouchDB 0.01640

Memcached 0.00015

Page 81: Introduction To MongoDB

The Space-Time Continuum

Space

Time

Page 82: Introduction To MongoDB

The Space-Time Continuum

Space

Time

Page 83: Introduction To MongoDB

Care and Feeding of a Mongod

Server

Laptop

Page 84: Introduction To MongoDB

Keeps your databasein memory

Data size

Page 85: Introduction To MongoDB

Keeps your databasein memory

Keep indexesin memory

Data size

Page 86: Introduction To MongoDB

Keeps your databasein memory

Keep indexesin memory

Keep the portion of the indexes you're using in memory

Data size

Page 87: Introduction To MongoDB

Keeps your databasein memory

Keep indexesin memory

Hits the disk andtakes forever.

Data size

Keep the portion of the indexes you're using in memory

Page 88: Introduction To MongoDB

Goals Easy-to-use

Fast

Always available

Easy to scale

Page 89: Introduction To MongoDB

master

slave slaveslave

Page 90: Introduction To MongoDB

master

slave

Replica Pairs

Page 91: Introduction To MongoDB

slave

Replica Pairs

Page 92: Introduction To MongoDB

master

Replica Pairs

Page 93: Introduction To MongoDB

master

slave

Replica Pairs

Page 94: Introduction To MongoDB

master

slave

Coming soon... replica sets

slave

slave

Page 95: Introduction To MongoDB

Eventual Consistency

Page 96: Introduction To MongoDB

Hey Twitter, I'm eating a donut.

Fred

Okay

Page 97: Introduction To MongoDB

Hey Twitter, I'm eating a donut.

Fail Whale

1,000,000x

Fred

Page 98: Introduction To MongoDB

Hey Twitter, I'm eating a donut.

Fred

Okay

Page 99: Introduction To MongoDB

Hey Twitter, I'm eating a donut.

Okay

Fred

Bob

Nothing new

from Fred

Page 100: Introduction To MongoDB

Hey Twitter, I'm eating a donut.

Fred

One sec

Page 101: Introduction To MongoDB

Bob

Wait a sec, Bob

Page 102: Introduction To MongoDB

Bob

I'd rather have Twitter up and know what Fred is doing later.

...eventual consistency works for your customers

Page 103: Introduction To MongoDB

Bob

I MUST KNOW WHAT FRED IS DOING RIGHT THIS SECOND!

...you need "real" consistency

Page 104: Introduction To MongoDB

Transactions

Page 105: Introduction To MongoDB

A Transaction

Insert this.

Okay, got it.

Phew, my data's safe.

Page 106: Introduction To MongoDB

A Transaction

Page 107: Introduction To MongoDB

A Transaction

Page 108: Introduction To MongoDB

Get Paranoid

Insert this.

Okay, got it.

Phew, my data's safe.

Page 109: Introduction To MongoDB

Get Paranoid

Page 110: Introduction To MongoDB

Get Paranoid

? I have no idea what

you're talking about.

Page 111: Introduction To MongoDB

Get Real Paranoid

Write this to disk

All over it!

I know better than

he does, I'll just

let this sit in a

buffer for a while.

Page 112: Introduction To MongoDB

Trust No One!

...trust a bunch of ones. Mostly.

Page 113: Introduction To MongoDB

Goals Easy-to-use

Fast

Always available

Easy to scale

Page 114: Introduction To MongoDB

WARNING!

α

Page 115: Introduction To MongoDB

shardshard

shard

shard

Page 116: Introduction To MongoDB
Page 117: Introduction To MongoDB

I want Bob's comments

Page 118: Introduction To MongoDB

Those are stored here.

Page 119: Introduction To MongoDB

Comments from Bob?

Here you go.

Page 120: Introduction To MongoDB

Server #3, you're

getting overloaded.

Configuration

Page 121: Introduction To MongoDB

Server #3, lockdown.

Split the data,

migrate half to the

new shard.

Router, server #3 has

A-M, server #5 has N-Z.

Page 122: Introduction To MongoDB
Page 123: Introduction To MongoDB
Page 124: Introduction To MongoDB

Configuration

Routers

Shards

Page 125: Introduction To MongoDB

Scaling

Page 126: Introduction To MongoDB

NoSQL LiveMarch 11in Boston

See www.10gen.com/events for details

Page 127: Introduction To MongoDB

Thank You!

www.mongodb.org

irc.freenode.net#mongodb

http://groups.google.com/group/mongodb-user

[email protected]

@kchodorow