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

Introduction to MongoDB on the Shell - Spencer Brody, Software Engineer, 10gen

mongodb
November 28, 2011

Introduction to MongoDB on the Shell - Spencer Brody, Software Engineer, 10gen

MongoDallas 2011

A general introduction to MongoDB followed by an interactive session using the MongoDB shell with the emphasis on queries and schema design.

mongodb

November 28, 2011
Tweet

More Decks by mongodb

Other Decks in Technology

Transcript

  1. Types of Non-Relational Data Models •  Key-value stores •  Document

    stores •  Column-oriented databases •  Graph databases
  2. Terminology RDBMS   Mongo   Table,  View   Collection  

    Row(s)   BSON  Document   Index   Index   Partition   Shard   Partition  Key   Shard  Key  
  3. Scaling MongoDB •  Replication – Availability and Durability •  Replica

    Sets •  Can also be used to scale reads •  Sharding – Read and write scalability •  Collections are sharded •  Each shard is served by its own replica set •  Shard key ranges are automatically balanced
  4. MongoDB is a Single-Master System •  All writes are to

    a primary (master) •  Failure of the primary is detected, and a new one is elected •  Application writes get an error if there is no quorum to elect a new master •  Reads can continue
  5. Single Master •  All writes go to a single master

    and then replicated •  Replication can provide read scalability •  Writing becomes a bottleneck •  Physical limitations (seek time) •  Throughput of a single I/O subsystem
  6. Single Master - Sharding •  Partition the primary key space

    •  Set up a duplicate system for each shard •  The write-rate limitation now applies to each shard •  Can the data be re-sharded on a live system? •  Can shards be re-balanced on a live system?
  7. Primary" Secondary" Secondary" Primary" Secondary" Secondary" Primary" Secondary" Secondary" Primary"

    Secondary" Secondary" Key Range" 0..30" Key Range" 31..60" Key Range" 61..90" Key Range" 91.. 100" MongoS" MongoS" MongoS" Read" Write" MongoS"
  8. Documents Blog Post Document" " p = { author: “Spencer”,"

    date: new Date()," title: “Introduction to MongoDB”," tags: [“NoSQL”, “Mongo”, “MongoDB”]}" " > db.posts.save(p)"
  9. Querying >db.posts.find()" " { _id : ObjectId("4c4ba5c0672c685e5e8aabf3")," author : “spencer",

    " date : “Mon Jul 11 2011 19:47:11 GMT-0700 (PDT)", " title: “Introduction to MongoDB”," tags: [“NoSQL”, “Mongo”, “MongoDB”]} " "
  10. Secondary Indexes Create index on any Field in Document" "

    // 1 means ascending, -1 means descending" " >db.posts.ensureIndex({author: 1})" " >db.posts.find({author: ʻspencer'}) " " { _id : ObjectId("4c4ba5c0672c685e5e8aabf3")," author : “spencer", " ... }"
  11. Query Operators •  Conditional Operators " •  $all, $exists, $mod,

    $ne, $in, $nin, $nor, $or, $size, $type" •  $lt, $lte, $gt, $gte" " // find posts with any tags " > db.posts.find( {tags: {$exists: true }} )" " // find posts matching a regular expression" > db.posts.find( {author: /^spe*/i } )" " // count posts by author" > db.posts.find( {author: ʻspencerʼ} ).count()"
  12. Atomic Operations •  $set, $unset, $inc, $push, $pushAll, $pull, $pullAll,

    $bit" > comment = { author: “fred”, " date: new Date()," text: “Interesting blog post”}" " > db.posts.update( { _id: “...” }, " " "{$push: {comments: comment}, " " " $inc: {numComments: 1}} );" "
  13. Nested Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), " author : “spencer","

    date : “Mon Jul 11 2011 19:47:11 GMT-0700 (PDT)", " text : “Introduction to MongoDB"," tags : [ “NoSQL", “Mongo", “MongoDB" ]," numComments : 1," comments : [" "{" " "author : "Fred"," " "date : “Mon Jul 11 2011 20:51:03 GMT-0700 (PDT)"," " "text : “Interesting blog post"" "}" ]}"
  14. Indexes // Index nested documents > db.posts.ensureIndex( “comments.author”:1 ) Ø db.posts.find({‘comments.author’:’Fred’})

    // Index on tags > db.posts.ensureIndex( tags: 1) > db.posts.find( { tags: ’Mongo’ } ) // geospatial index > db.posts.ensureIndex( “author.location”: “2d” ) > db.posts.find( “author.location” : { $near : [22,42] } )
  15. MongoDB – More •  Geo-spatial queries •  Require a geo

    index •  Find points near a given point •  Find points within a polygon/sphere •  Built-in Map-Reduce •  The caller provides map and reduce functions written in JavaScript
  16. MongoDB Access •  Drivers are available in many languages • 

    10gen supported •  C, C# (.Net), C++, Java, JavaScript, Perl, PHP, Python, Ruby, Scala •  Community supported •  Clojure, ColdFusion, F#, Go, Groovy, Lua, Node.js, R •  http://www.mongodb.org/display/DOCS/Overview+- +Writing+Drivers+and+Tools
  17. MongoDB Availability •  Source •  https://github.com/mongodb/mongo •  Server •  License:

    AGPL •  http://www.mongodb.org/downloads •  Drivers •  License: Apache •  http://www.mongodb.org/display/DOCS/Drivers
  18. @mongodb   ©  Copyright  2010  10gen  Inc.   conferences,  appearances,

     and  meetups   http://www.10gen.com/events     http://bit.ly/mongoO     Facebook                    |                  Twitter                  |                  LinkedIn   http://linkd.in/joinmongo   download at mongodb.org We’re  Hiring  !   Engineers,  Sales,  Evangelist,  Marketing,  Support,  Developers