Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB

 MongoDB

Presentación de MongoDB por Christian Kvalheim (@christkv) de 10gen en el Betabeers Barcelona celebrado el 18 de mayo de 2012.

Avatar for Betabeers

Betabeers

May 19, 2012
Tweet

More Decks by Betabeers

Other Decks in Technology

Transcript

  1. Today's Talk • Quick introduction to NoSQL • Some Background

    about mongoDB • Using mongoDB • Deploying mongoDB Friday, May 18, 12
  2. Key-Value Stores •A mapping from a key to a value

    •The store doesn't know anything about the the key or value •The store doesn't know anything about the insides of the value •Operations • Set, get, or delete a key-value pair Friday, May 18, 12
  3. Column-Oriented Stores •Like a relational store, but flipped around: all

    data for a column is kept together • An index provides a means to get a column value for a record •Operations: • Get, insert, delete records; updating fields • Streaming column data in and out of Hadoop Friday, May 18, 12
  4. Graph Databases •Stores vertex-to-vertex edges •Operations: • Getting and setting

    edges • Sometimes possible to annotate vertices or edges • Query languages support finding paths between vertices, subject to various constraints Friday, May 18, 12
  5. Document Stores •The store is a container for documents •

    Documents are made up of named fields • Fields may or may not have type definitions • e.g. XSDs for XML stores, vs. schema-less JSON stores •Can create "secondary indexes" • These provide the ability to query on any document field(s) •Operations: • Insert and delete documents • Update fields within documents Friday, May 18, 12
  6. What is mongoDB? MongoDB is a scalable, high-performance, open source

    NoSQL database. •Document-oriented storage •Full Index Support •Replication & High Availability •Auto-Sharding •Querying •Fast In-Place Updates •Map/Reduce •GridFS Friday, May 18, 12
  7. • Company behind mongoDB – (A)GPL license, own copyrights, engineering

    team – support, consulting, commercial license • Management – Google/DoubleClick, Oracle, Apple, NetApp – Funding: Sequoia, Union Square, Flybridge – Offices in NYC, Palo Alto, London, Dublin – 100+ employees Friday, May 18, 12
  8. Where can you use it? MongoDB is Implemented in C++

    • Platforms 32/64 bit Windows, Linux, Mac OS-X, FreeBSD, Solaris Drivers are available in many languages 10gen supported • C, C# (.Net), C++, Erlang, Haskell, Java, JavaScript, Perl, PHP, Python, Ruby, Scala, Node.JS Community supported • Clojure, ColdFusion, F#, Go, Groovy, Lua, R ... http://www.mongodb.org/display/DOCS/Drivers Friday, May 18, 12
  9. History •First release – February 2009 •v1.0 - August 2009

    •v1.2 - December 2009 – MapReduce, ++ •v1.4 - March 2010 – Concurrency, Geo •V1.6 - August 2010 – Sharding, Replica Sets •V1.8 – March 2011 – Journaling, Geosphere •V2.0 - Sep 2011 – V1 Indexes, Concurrency •V2.2 - Soon - Aggregation, Concurrency Friday, May 18, 12
  10. Terminology RDBMS MongoDB Table Collection Row(s) JSON Document Index Index

    Join Embedding & Linking Partition Shard Partition Key Shard Key Friday, May 18, 12
  11. > p = { author: "Chris", date: new ISODate(), text:

    "About MongoDB...", tags: ["tech", "databases"]} > db.posts.save(p) Documents Blog Post Document Friday, May 18, 12
  12. > db.posts.find() { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Chris", date

    : ISODate("2012-02-02T11:52:27.442Z"), text : "About MongoDB...", tags : [ "tech", "databases" ] } Querying Notes: _id is unique, but can be anything you'd like Friday, May 18, 12
  13. Introducing BSON JSON has powerful, but limited set of datatypes

    • arrays, objects, strings, numbers and null BSON is a binary representation of JSON • Adds extra dataypes with Date, Int types, Id, … • Optimized for performance and navigational abilities • And compression MongoDB sends and stores data in BSON • bsonspec.org Friday, May 18, 12
  14. // 1 means ascending, -1 means descending > db.posts.ensureIndex({author: 1})

    > db.posts.findOne({author: 'Chris'}) { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author: "Chris", ... } Secondary Indexes Create index on any Field in Document Friday, May 18, 12
  15. // 1 means ascending, -1 means descending > db.posts.ensureIndex({author: 1,

    ts: -1}) > db.posts.find({author: 'Chris'}).sort({ts: -1}) [{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author: "Chris", ...}, { _id : ObjectId("4f61d325c496820ceba84124"), author: "Chris", ...}] Compound Indexes Create index on multiple fields in a Document Friday, May 18, 12
  16. // find posts with any tags > db.posts.find({tags: {$exists: true

    }}) // find posts matching a regular expression > db.posts.find({author: /^ro*/i }) // count posts by author > db.posts.find({author: 'Chris'}).count() Query Operators Conditional Operators - $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size, $type - $lt, $lte, $gt, $gte Friday, May 18, 12
  17. > db.posts.find({"author": 'Ross'}).explain() { "cursor" : "BtreeCursor author_1", "nscanned" :

    1, "nscannedObjects" : 1, "n" : 1, "millis" : 0, "indexBounds" : { "author" : [ [ "Chris", "Chris" ] ] } } Examine the query plan Friday, May 18, 12
  18. // Create a comment > new_comment = { author: "Fred",

    date: new Date(), text: "Best Post Ever!"} // Add to post > db.posts.update({ _id: "..." }, {"$push": {comments: new_comment}, "$inc": {comments_count: 1} }); Atomic Operations $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit Friday, May 18, 12
  19. { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Chris", date : "Thu

    Feb 02 2012 11:50:01", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [{ author : "Fred", date : "Fri Feb 03 2012 13:23:11", text : "Best Post Ever!" }], comment_count : 1 } Nested Documents Friday, May 18, 12
  20. { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Chris", date : "Thu

    Feb 02 2012 11:50:01", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [{ author : "Fred", date : "Fri Feb 03 2012 13:23:11", text : "Best Post Ever!" }], comment_count : 1 } Nested Documents Friday, May 18, 12
  21. // Index nested documents > db.posts.ensureIndex("comments.author": 1) > db.posts.find({"comments.author": "Fred"})

    // Index on tags (multi-key index) > db.posts.ensureIndex( tags: 1) > db.posts.find( { tags: "tech" } ) Secondary Indexes Friday, May 18, 12
  22. Geo • Geo-spatial queries • Require a geo index •

    Find points near a given point • Find points within a polygon/sphere // geospatial index > db.posts.ensureIndex( "author.location": "2d" ) > db.posts.find( "author.location" : { $near : [22, 42] } ) Friday, May 18, 12
  23. Map Reduce The caller provides map and reduce functions written

    in JavaScript // Emit each tag > map = "this['tags'].forEach( function(item) {emit(item, 1);} );" // Calculate totals > reduce = "function(key, values) { var total = 0; var valuesSize = values.length; for (var i=0; i < valuesSize; i++) { total += parseInt(values[i], 10); } return total; }; Friday, May 18, 12
  24. // run the map reduce > db.posts.mapReduce(map, reduce, {"out": {

    inline : 1}}); { "results" : [ {"_id" : "databases", "value" : 1}, {"_id" : "tech", "value" : 1 } ], "timeMillis" : 1, "counts" : { "input" : 1, "emit" : 2, "reduce" : 0, "output" : 2 }, "ok" : 1, } Map Reduce Friday, May 18, 12
  25. // Count tags > agg = db.posts.aggregate( {$unwind: "$tags"}, {$group

    : {_id : "$tags", count : {$sum: 1}}} ) > agg.result [{"_id": "databases", "count": 1}, {"_id": "tech", "count": 1}] Aggregation - coming in 2.2 Friday, May 18, 12
  26. // (Python) Create a new instance of GridFS >>> fs

    = gridfs.GridFS(db) // Save file to mongo >>> my_image = open('my_image.jpg', 'r') >>> file_id = fs.put(my_image) // Read file >>> fs.get(file_id).read() GridFS Save files in mongoDB Stream data back to the client Friday, May 18, 12
  27. { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), line_items : [ { sku: 'tt-123',

    name: 'Coltrane: Impressions' }, { ski: 'tt-457', name: 'Davis: Kind of Blue' } ], address : { name: 'Banker', street: '111 Main', zip: 10010 }, payment: { cc: 4567, exp: Date(2012, 7, 7) }, subtotal: 2355 } Rich Documents Friday, May 18, 12
  28. Deployment •Single server - need a strong backup plan •Replica

    sets - High availability - Automatic failover P P S S Friday, May 18, 12
  29. Deployment •Single server - need a strong backup plan •Replica

    sets - High availability - Automatic failover •Sharded - Horizontally scale - Auto balancing P S S P S S P P S S Friday, May 18, 12
  30. MongoDB Use Cases •Archiving •Content Management •Ecommerce •Finance •Gaming •Government

    •Metadata Storage •News & Media •Online Advertising •Online Collaboration •Real-time stats/analytics •Social Networks •Telecommunications Friday, May 18, 12
  31. @mongodb conferences, appearances, and meetups http://www.10gen.com/events http://bit.ly/mongofb Facebook | Twitter

    | LinkedIn http://linkd.in/joinmongo download at mongodb.org support, training, and this talk brought to you by Friday, May 18, 12