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


         Kristina Chodorow
Who am I?

Software Engineer at




           Here from New York City
Why Use
Non-Relational DBs?
CAP
• Consistency
    nome : "joe"         nome : "joe"



• Availability


• Partitioning
Key-Value Stores

    Key            Value
    foo            bar
    x              123
    ts             6:13:26 12/3/1...
Key-Value Stores




•Fast     •Too simple
•Simple
Column-Oriented
Cassandra
Document-Oriented
{
    date : Date(2007-05-07),
    time : {
        start : 9.25,
        end : 10.25
    }
    sum : 0,...
Consistency
Availability
Partitioning
Consistency
Availability   master

Partitioning



               slave
Consistency
Availability
Partitioning


               slave
Consistency
Availability
Partitioning


               master
Consistency
Availability    slave
Partitioning


               master
Consistency
Availability
Partitioning                       Pair


                          Router

                     ...
Introduction to MongoDB
A JavaScript Database

$ mongodb-linux-1.0/bin/mongo
MongoDB shell version: 1.0.0
url: test
connecting to: test
type "help...
JSON and BSON
Strict JSON types:
{
  x : null, y : true, z : 123, w : "xyz",
  a : { b : 1 }, c : [1, 2, 3]
}

Mongo JSON ...
Collections, not Tables
Collections

{
    date : Date(2007-05-07),
    time : {
        start : 9.25,
        end : 10.25
    }
    sum : 0,
    ...
Using the Shell

> db
test
> use xyz
> db
xyz
> db.splorch.find()
>
Inserting

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

> db.foo.find({name : "Joe"})
{
    "_id" :
  ObjectId("2fe3e4d892aa...
Object Ids
            an autogenerated primary key


"_id" : ObjectId("2fe3e4d892aa73234c910bed")


12 bytes:

    2fe3e4...
Nested Objects

> db.blog.insert({title : "First Post",
    content : "Hello, world!",
    author : {name : "Joe", id : 12...
Querying
posts = db.blog.find({
    "author" : "Joe"})

commentsByFred = db.blog.find({
    "comments.author" : "Fred"})

...
Speaking of indexing…
db.people.ensureIndex({"age" : 1});

db.people.ensureIndex({
    "name" : -1,
    "ts" : -1,
    "co...
Updating

db.blog.update({title : "First Post"}, {
  $push : {
    comments : {
      author : "Fred",
      comment : "Du...
…which gives us:
> db.blog.findOne()
{
    _id : ObjectId("4ae06192213900000000745c"),
    "title" : "First Post",
    "co...
$ instead of >, <, =, etc.

$gt, $gte, $lt, $lte, $eq, $neq, $exists,
$set, $mod, $where, $in, $nin, $inc
$push, $pull, $p...
$where
db.blog.findOne({$where :
    'this.y == (this.x + this.z)'});

Will work:
{"x" : 1, "y" : 4, "z" : 3}
{"x" : "hi",...
Optimizing $where
db.blogs.findOne({
    name : "Sally",
    age : {'$gt' : 18},
    $where : 'Array.sort(this.interests)[...
Cursors
cursor = db.blah.find(array("foo" : "bar"))

while (cursor.hasNext()) {
    obj = cursor.next()
}
Applications
soliMAP
           @trackmeet




                                  FetLife

Dextify2
Paging
cursor = db.results.find()
    .sort({"ts" : -1})
    .skip(page_num * results_per_page)
    .limit(results_per_pag...
Logging

•   insert/update/remove is fast
•   Capped collections
•   Schemaless
•   $inc for counts
Storing Files
Max: 4Mb
Storing Files




(More than 4 Mb)
Storing Files


J   J   J

                            chunks
J   J   J


J   J   J           _id :   J        files
Storing Files
ObjectId fileId = new ObjectId();

fileObj = {
    _id : fileId,
    filename : "ggbridge.png",
    user : "...
Aggregation
group =~ GROUP BY

Map/Reduce
db.runCommand({
    mapreduce : <collection>,
    map : <mapfunction>,
    reduc...
www.mongodb.org
Drivers




C#, Erlang, Factor
Thank you!

  kristina@10gen.com
      @kchodorow

        @mongodb
irc.freenode.net#mongodb
     www.mongodb.org
Latinoware
Latinoware
Latinoware
Latinoware
Upcoming SlideShare
Loading in...5
×

Latinoware

962

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
962
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Latinoware

  1. 1. base de dados orientada a documentos para aplicações Web Kristina Chodorow
  2. 2. Who am I? Software Engineer at Here from New York City
  3. 3. Why Use Non-Relational DBs?
  4. 4. CAP • Consistency nome : "joe" nome : "joe" • Availability • Partitioning
  5. 5. Key-Value Stores Key Value foo bar x 123 ts 6:13:26 12/3/1945 data x9Fx44x1e Tokyo Cabinet, Dynamo, MemcacheDB
  6. 6. Key-Value Stores •Fast •Too simple •Simple
  7. 7. Column-Oriented Cassandra
  8. 8. Document-Oriented { date : Date(2007-05-07), time : { start : 9.25, end : 10.25 } sum : 0, comments : ["task 1", "run 4", "testing module"] } vs.
  9. 9. Consistency Availability Partitioning
  10. 10. Consistency Availability master Partitioning slave
  11. 11. Consistency Availability Partitioning slave
  12. 12. Consistency Availability Partitioning master
  13. 13. Consistency Availability slave Partitioning master
  14. 14. Consistency Availability Partitioning Pair Router Pair Pair ...eventual consistency
  15. 15. Introduction to MongoDB
  16. 16. A JavaScript Database $ mongodb-linux-1.0/bin/mongo MongoDB shell version: 1.0.0 url: test connecting to: test type "help" for help >
  17. 17. JSON and BSON Strict 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() }
  18. 18. Collections, not Tables
  19. 19. Collections { date : Date(2007-05-07), time : { start : 9.25, end : 10.25 } sum : 0, comments : ["task 1", "run 4"] }
  20. 20. Using the Shell > db test > use xyz > db xyz > db.splorch.find() >
  21. 21. Inserting > db.foo.insert({name : "Joe", age : 34}) > db.foo.find({name : "Joe"}) { "_id" : ObjectId("2fe3e4d892aa73234c910bed"), "name" : "Joe", "age" : 34 }
  22. 22. Object Ids an autogenerated primary key "_id" : ObjectId("2fe3e4d892aa73234c910bed") 12 bytes: 2fe3e4d892aa73234c910bed |------||----||--||----| ts mac pid inc
  23. 23. Nested Objects > db.blog.insert({title : "First Post", content : "Hello, world!", author : {name : "Joe", id : 123}, comments : [] })
  24. 24. Querying posts = db.blog.find({ "author" : "Joe"}) commentsByFred = db.blog.find({ "comments.author" : "Fred"}) commentedByFred = db.blog.find({ "comments.author" : /fred/i})
  25. 25. Speaking of indexing… db.people.ensureIndex({"age" : 1}); db.people.ensureIndex({ "name" : -1, "ts" : -1, "comments.author" : 1 });
  26. 26. Updating db.blog.update({title : "First Post"}, { $push : { comments : { author : "Fred", comment : "Dumb post." } } });
  27. 27. …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" }] }
  28. 28. $ 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}})
  29. 29. $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}
  30. 30. Optimizing $where db.blogs.findOne({ name : "Sally", age : {'$gt' : 18}, $where : 'Array.sort(this.interests)[2] == "volleyball"'});
  31. 31. Cursors cursor = db.blah.find(array("foo" : "bar")) while (cursor.hasNext()) { obj = cursor.next() }
  32. 32. Applications
  33. 33. soliMAP @trackmeet FetLife Dextify2
  34. 34. Paging cursor = db.results.find() .sort({"ts" : -1}) .skip(page_num * results_per_page) .limit(results_per_page);
  35. 35. Logging • insert/update/remove is fast • Capped collections • Schemaless • $inc for counts
  36. 36. Storing Files Max: 4Mb
  37. 37. Storing Files (More than 4 Mb)
  38. 38. Storing Files J J J chunks J J J J J J _id : J files
  39. 39. 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> }
  40. 40. Aggregation group =~ GROUP BY Map/Reduce db.runCommand({ mapreduce : <collection>, map : <mapfunction>, reduce : <reducefunction> [, query : <query filter object>] [, out : <outputcollectionname>] [, keeptemp: <true|false>] [, finalize : <finalizefunction>] })
  41. 41. www.mongodb.org
  42. 42. Drivers C#, Erlang, Factor
  43. 43. Thank you! kristina@10gen.com @kchodorow @mongodb irc.freenode.net#mongodb www.mongodb.org
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×