latinoware

47
base de dados orientada a documentos para aplicações Web Kristina Chodorow

Upload: kchodorow

Post on 25-May-2015

953 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Latinoware

base de dados orientada a

documentos para aplicações Web

Kristina Chodorow

Page 2: Latinoware

Who am I?

Software Engineer at

Here from New York City

Page 3: Latinoware

Why Use

Non-Relational DBs?

Page 4: Latinoware
Page 5: Latinoware
Page 6: Latinoware

CAP

• Consistency

• Availability

• Partitioning

nome : "joe" nome : "joe"

Page 7: Latinoware

Key-Value Stores

Key Value

foo bar

x 123

ts 6:13:26 12/3/1945

data \x9F\x44\x1e

Tokyo Cabinet, Dynamo, MemcacheDB

Page 8: Latinoware

Key-Value Stores

•Fast

•Simple•Too simple

Page 9: Latinoware

Column-Oriented

Cassandra

Page 10: Latinoware

Document-Oriented

{

date : Date(2007-05-07),

time : {

start : 9.25,

end : 10.25

}

sum : 0,

comments : ["task 1", "run 4", "testing module"]

}

vs.

Page 11: Latinoware

Consistency

Availability

Partitioning

Page 12: Latinoware

master

slave

Consistency

Availability

Partitioning

Page 13: Latinoware

slave

Consistency

Availability

Partitioning

Page 14: Latinoware

master

Consistency

Availability

Partitioning

Page 15: Latinoware

master

slave

Consistency

Availability

Partitioning

Page 16: Latinoware

Router

Pair

Pair

Pair

Consistency

Availability

Partitioning

...eventual consistency

Page 17: Latinoware
Page 18: Latinoware
Page 19: Latinoware

Introduction to MongoDB

Page 20: Latinoware

A JavaScript Database

$ mongodb-linux-1.0/bin/mongo

MongoDB shell version: 1.0.0

url: test

connecting to: test

type "help" for help

>

Page 21: Latinoware

JSON and BSONStrict JSON types:

{

x : null, y : true, z : 123, w : "xyz",

a : { b : 1 }, c : [1, 2, 3]

}

Mongo JSON adds:

{

ts : new Date(),

query : /regex/ig,

_id : new ObjectId()

}

Page 22: Latinoware

Collections, not Tables

Page 23: Latinoware

Collections

{

date : Date(2007-05-07),

time : {

start : 9.25,

end : 10.25

}

sum : 0,

comments : ["task 1", "run 4"]

}

Page 24: Latinoware

Using the Shell

> db

test

> use xyz

> db

xyz

> db.splorch.find()

>

Page 25: Latinoware

Inserting

> db.foo.insert({name : "Joe", age : 34})

> db.foo.find({name : "Joe"})

{

"_id" :

ObjectId("2fe3e4d892aa73234c910bed"),

"name" : "Joe",

"age" : 34

}

Page 26: Latinoware

Object Idsan autogenerated primary key

"_id" : ObjectId("2fe3e4d892aa73234c910bed")

12 bytes:

2fe3e4d892aa73234c910bed

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

ts mac pid inc

Page 27: Latinoware

Nested Objects

> db.blog.insert({title : "First Post",

content : "Hello, world!",

author : {name : "Joe", id : 123},

comments : []

})

Page 28: Latinoware

Querying

posts = db.blog.find({

"author" : "Joe"})

commentsByFred = db.blog.find({

"comments.author" : "Fred"})

commentedByFred = db.blog.find({

"comments.author" : /fred/i})

Page 29: Latinoware

Speaking of indexing…

db.people.ensureIndex({"age" : 1});

db.people.ensureIndex({

"name" : -1,

"ts" : -1,

"comments.author" : 1

});

Page 30: Latinoware

Updating

db.blog.update({title : "First Post"}, {

$push : {

comments : {

author : "Fred",

comment : "Dumb post."

}

}

});

Page 31: Latinoware

…which gives us:

> db.blog.findOne()

{

_id : ObjectId("4ae06192213900000000745c"),

"title" : "First Post",

"content" : "Hello, world!"

"author" : {"name" : "Joe", "id" : 123}

"comments" : [{

"author" : "Fred",

"comment" : "Dumb post"

}]

}

Page 32: Latinoware

$ instead of >, <, =, etc.

$gt, $gte, $lt, $lte, $eq, $neq, $exists,

$set, $mod, $where, $in, $nin, $inc

$push, $pull, $pop, $pushAll, $popAll

db.foo.bar.find({x : {$gt : 4}})

Page 33: Latinoware

$where

db.blog.findOne({$where :

'this.y == (this.x + this.z)'});

Will work:

{"x" : 1, "y" : 4, "z" : 3}

{"x" : "hi", "y" : "hibye", "z" : "bye"}

Won’t work:

{"x" : 1, "y" : 1}

Page 34: Latinoware

Optimizing $where

db.blogs.findOne({

name : "Sally",

age : {'$gt' : 18},

$where : 'Array.sort(this.interests)[2]

== "volleyball"'});

Page 35: Latinoware

Cursors

cursor = db.blah.find(array("foo" : "bar"))

while (cursor.hasNext()) {

obj = cursor.next()

}

Page 36: Latinoware

Applications

Page 37: Latinoware

Dextify2

FetLife

soliMAP

@trackmeet

Page 38: Latinoware

Paging

cursor = db.results.find()

.sort({"ts" : -1})

.skip(page_num * results_per_page)

.limit(results_per_page);

Page 39: Latinoware

Logging

• insert/update/remove is fast

• Capped collections

• Schemaless

• $inc for counts

Page 40: Latinoware

Storing Files

Max: 4Mb

Page 41: Latinoware

Storing Files

(More than 4 Mb)

Page 42: Latinoware

Storing Files

chunks

JJ J

J

JJ

J

J

J

J_id :files

Page 43: Latinoware

Storing Files

ObjectId fileId = new ObjectId();

fileObj = {

_id : fileId,

filename : "ggbridge.png",

user : "joe",

takenIn : "San Francisco"

}

chunkObj = {

fileId : fileId,

chunkNum : N

data : <binary data>

}

Page 44: Latinoware

Aggregation

group =~ GROUP BY

Map/Reducedb.runCommand({

mapreduce : <collection>,

map : <mapfunction>,

reduce : <reducefunction>

[, query : <query filter object>]

[, out : <outputcollectionname>]

[, keeptemp: <true|false>]

[, finalize : <finalizefunction>]

})

Page 45: Latinoware

www.mongodb.org

Page 46: Latinoware

Drivers

C#, Erlang, Factor

Page 47: Latinoware

Thank you!

[email protected]

@kchodorow

@mongodb

irc.freenode.net#mongodb

www.mongodb.org