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

Introduction to MongoDB

Introduction to MongoDB

Introduction to MongoDB presentation by Shiva Thirumazhusai

mongodb

July 07, 2011
Tweet

More Decks by mongodb

Other Decks in Programming

Transcript

  1. January 19, 2011 Slide 3 Why MongoDB ? Document-oriented 

    Documents (objects) map nicely to programming language data types  Embedded documents and arrays reduce need for joins  Dynamically-typed (schemaless) for easy schema evolution  No joins and no (multi-object) transactions for high performance and easy scalability High performance  No joins and no transactions makes reads and writes fast  Indexes with indexing into embedded documents and arrays  Optional asynchronous writes High availability  Replicated servers with automatic master failover Easy scalability  Eventually-consistent reads are distributed over replicated servers  Automatic sharding (auto-partitioning of data across servers)  Reads and writes are distributed over shards  No joins and no transactions make distributed queries easy and fast Rich query language 3 Sunday, January 30, 2011
  2. Key Features January 19, 2011 Slide 4 Schemaless Horizontally scalable

    Focused on performance 4 Sunday, January 30, 2011
  3. Mongo data model January 19, 2011 Slide 6 A Mongo

    system holds a set of databases A database holds a set of collections A collection holds a set of documents A document is a set of fields A field is a key-value pair A key is a name (string) A value is a  basic type like string, integer, float, timestamp, binary, etc.,  a document, or  an array of values 6 Sunday, January 30, 2011
  4. Query operators January 19, 2011 Slide 9 $ne $not $in

    $or $nin $gt $mod $gte $all $lt $size $lte $exists $elemMatch 9 Sunday, January 30, 2011
  5. Update operators January 19, 2011 Slide 11 $inc $set $push

    $pushAll $pop $pull $pullAll $addToSet 11 Sunday, January 30, 2011
  6. A MongoDB Ecosystem January 19, 2011 Slide 13 Officially‐supported drivers

    Java Ruby PHP Python Perl C# C,C++ 13 Sunday, January 30, 2011
  7. Community Support January 19, 2011 Slide 14 Community‐supported drivers Scala(mostJVM

    langauges) Node.js Erlang Haskell Smalltalk Lua Go 14 Sunday, January 30, 2011
  8. JSON-style Documents represented as BSON January 19, 2011 Slide 16

    {“hello” : “world”} \x16\x00\x00\x00\x02hello \x00\x06\x00\x00\x00world \x00\x00 16 Sunday, January 30, 2011
  9. Flexible “Schemas” January 19, 2011 Slide 20 {“author”: “mike”, “text”:

    “...”} {“author”: “eliot”, “text”: “...”, “tags”: [“mongodb”]} 19 Sunday, January 30, 2011
  10. Unique ID January 19, 2011 Slide 21 Special key Present

    in all documents Unique across a Collection Any type you want _id 20 Sunday, January 30, 2011
  11. Post January 19, 2011 Slide 22 {author: “mike”, date: new

    Date(), text: “my blog post...”, Tags: [“mongodb”, “intro”]} 21 Sunday, January 30, 2011
  12. Comment January 19, 2011 Slide 23 {author: “eliot”, date: new

    Date(), text: “great post!”} 22 Sunday, January 30, 2011
  13. New Post January 19, 2011 Slide 24 post = {author:“mike”,

    date: new Date(), text: “my blog post...”, tags: [“mongodb”, “intro”]} db.posts.save(post) 23 Sunday, January 30, 2011
  14. Embedding a Comment January 19, 2011 Slide 25 c =

    {author: “eliot”, date: new Date(), text: “great post!”} db.posts.update({_id: post._id}, {$push: {comments: c}}) 24 Sunday, January 30, 2011
  15. Posts Since April 1 January 19, 2011 Slide 28 april_1

    = new Date(2010, 3, 1) db.posts.find({date: {$gt: april_1}}) 27 Sunday, January 30, 2011
  16. Posts With a Tag January 19, 2011 Slide 30 db.posts.find({tags:“mongodb”})

    ...and Fast (multi‐keyindexes) db.posts.ensureIndex({tags:1}) 29 Sunday, January 30, 2011
  17. Indexing / Querying on Embedded Docs January 19, 2011 Slide

    31 db.posts.ensureIndex({“comments.author”: 1}) db.posts.find({“comments.author”: “eliot”}) 30 Sunday, January 30, 2011
  18. Basic Paging January 19, 2011 Slide 33 page = 2

    page_size = 15 db.posts.find().limit(page_size) .skip(page * page_size) 32 Sunday, January 30, 2011
  19. Migration: Adding Titles (just start adding them) January 19, 2011

    Slide 34 post = {author: “mike”, date: new Date(), text: “another blog post...”, tags: [“mongodb”], title: “MongoDB for Fun and Profit”} post_id = db.posts.save(post) 33 Sunday, January 30, 2011
  20. Advanced Queries January 19, 2011 Slide 35 $gt, $lt, $gte,

    $lte, $ne, $all, $in, $nin db.posts.find({$where: “this.author == ‘mike’ || this.title == ‘foo’”}) 34 Sunday, January 30, 2011
  21. SQL to Mongo Mapping Chart - 1 January 19, 2011

    Slide 36 35 Sunday, January 30, 2011
  22. SQL to Mongo Mapping Chart - 1 January 19, 2011

    Slide 37 36 Sunday, January 30, 2011
  23. SQL to Mongo Mapping Chart - 1 January 19, 2011

    Slide 38 37 Sunday, January 30, 2011
  24. Resources January 19, 2011 Slide 19 Documentation http://mongodb.org Books http://10gen.com/books

    MongoDB User List mongodb‐user Google group IRC #mongodb Support [email protected] 40 Sunday, January 30, 2011
  25. Object ID Specification BSON ObjectID Specification A BSON ObjectID is

    a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order. Here's the schema: 0 1 2 3 4 5 6 7 8 9 10 11 time time time time machine machine machine pid pid inc inc inc 41 Sunday, January 30, 2011