Introduction to MongoDB

1,541 views

Published on

An introduction to MongoDB as shown at the Madison MongoDB User Group (MadMUG) meetup on 4/16/2012.

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

No Downloads
Views
Total views
1,541
On SlideShare
0
From Embeds
0
Number of Embeds
77
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • - Developer at heart\n- 15 years experience\n- Responsible for selecting Mongo\n\n
  • \n
  • - Bottom-up attempt to improve student outcomes through disruptive change outside of the education system. \n- Allows students to create and store lecture notes and flashcards and access them online and via mobile apps (iOS and Android)\n
  • - No public numbers\n- Thousands of simultaneous users at peak\n
  • \n
  • \n
  • Also called “non-relational databases”\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • update() defaults to updating ~only~ the first matching document unless “multi” is set to true.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Introduction to MongoDB

    1. 1. StudyBlueIntroduction to MongoDBSean LaurentApril 16, 2012StudyBlue, Inc.
    2. 2. Overview • NoSQL landscape • MongoDB features • Availability & Scalability • Useful Tools • Strengths & WeaknessesStudyBlue, Inc.
    3. 3. Who am I? • Sean Laurent • sean@studyblue.com • Director of Operations at StudyBlue, Inc.StudyBlue, Inc.
    4. 4. studyblue.comStudyBlue, Inc.
    5. 5. About StudyBlue • Bottom-up attempt to improve student outcomes • Online service for storing, studying, sharing and ultimately mastering course material • Digital backpack for studentsStudyBlue, Inc.
    6. 6. StudyBlue Usage • Many simultaneous users • Rapid growth • Cyclical usageStudyBlue, Inc.
    7. 7. NoSQL Landscape:Database ModelsStudyBlue, Inc.
    8. 8. Database ModelsStudyBlue, Inc.
    9. 9. NoSQL Data Models • Key-Value stores • Document stores • Column-oriented databases • Graph databasesStudyBlue, Inc.
    10. 10. Key-Value Stores • Maps keys to values • Operations: • Get, Set and Delete • Examples: • Cassandra, Project Voldemort, memcache, RedisStudyBlue, Inc.
    11. 11. Document Stores • Contains “documents” • Documents have “fields” • Secondary indexes • Operations: • Insert, Update, Delete, Update fields • Examples: • MongoDB, CouchDBStudyBlue, Inc.
    12. 12. Column Oriented Databases • Reversed relational database • Data for column kept together • Operations: • Insert, Update, Delete; update fields • Examples: • BigTable, HBase, CStore, GreenplumStudyBlue, Inc.
    13. 13. Graph Databases • Stores edges between nodes • Operations: • Getting/Setting edges • Annotate vertices or edges • Query for paths • Examples: • GraphBase, Neo4j, VertexDBStudyBlue, Inc.
    14. 14. MongoDB:FeaturesStudyBlue, Inc.
    15. 15. Document Oriented • System holds databases • A database holds collections • A collection holds documents • A document is a set of fields written in JSON • A field is key-value pair • A key is a string • A value is a basic type, an array or another documentStudyBlue, Inc.
    16. 16. Sample Document { “author” : “sean”, “title” : “Intro to MongoDB”, “tags” : [ “mongodb”, “introduction” ], “views” : 42, “comments” : [ { “author” : “bob”, “comment” : “Best presentation EVER!” }, { “author” : “tom”, “comment” : “Way too long winded.” } ] }StudyBlue, Inc.
    17. 17. Flexible Schemas• Different documents in one collection can have different fields: { “author” : “sean”, “title” : “Intro to MongoDB” } { “author” : “sean”, “title” : “Databases At Scale”, “tags” : [ “mongodb”, “scaling” ] }StudyBlue, Inc.
    18. 18. Query Features • No joins • No transactions across documents or collections • Atomic updates • Simple query syntaxStudyBlue, Inc.
    19. 19. Query Examples db.posts.find({author: ‘sean’}) db.posts.find({author: ‘sean’}).sort({timestamp: 1}) db.posts.find({author: ‘sean’}).sort({timestamp: 1}).limit(2) db.posts.find({author: ‘sean’, tags: ‘mongodb’}) db.posts.find({author: ‘sean’, tags: {$in: [‘mongodb’, ‘intro’]}}) db.posts.find({author: ‘sean’, views: {$gt: 10}}) db.posts.find({$or: [{author: ‘sean’}, {views: ${gt: 20}]}) db.posts.find({}, {author: 1, views: 1})StudyBlue, Inc.
    20. 20. Update Examples db.posts.update({author: ‘sean’}, { $incr: {views: 1}}) db.posts.update({author: ‘sean’}, { $set: {views: 350}}) db.posts.update({author: ‘sean’}, { $push: {tags: ‘awesome’}}) db.posts.update({author: ‘sean’}, { $set: {views: 350}}, /* upsert */ true) db.posts.update({author: ‘sean’}, { $incr: {views: 1}}, /* upsert */ false, /* multi */ true)StudyBlue, Inc.
    21. 21. Indexes • B-Tree indexes • Embedded fields • Compound keys • Sparse • Unique • v2.1.0 or later support background indexingStudyBlue, Inc.
    22. 22. Official Drivers • C • C++ • Erlang • Haskell • Java • Javascript • .NET (C#, F#, PowerShell) • Perl • PHP • Python • Ruby • ScalaStudyBlue, Inc.
    23. 23. Community Supported Drivers • ActionScript 3 • Clojure • Dart • Go • Groovy • node.js • Objective C • Rails 3 • ...StudyBlue, Inc.
    24. 24. Other Cool Stuff • Aggregation • Explain • Map Reduce • Capped collections • GridFS • Geospatial indexingStudyBlue, Inc.
    25. 25. MongoDB:Availability &ScalabilityStudyBlue, Inc.
    26. 26. Replication • Replica sets • Asynchronous master/slave replication • Consists of two or more nodes that are copies of each other • Eventual consistency • Automatic failover • Automatically elects a primary (aka master) • Automatic recoveryStudyBlue, Inc.
    27. 27. Replica Set Benefits • Redundancy / Disaster Recovery • Automated failover • Read scaling • MaintenanceStudyBlue, Inc.
    28. 28. Replica Set - Read ScalingStudyBlue, Inc.
    29. 29. Replica Set Elections • Consensus Vote • Arbiters • Priorities • By default, all nodes have equal priority • Assign custom priorities for more powerful machines or specific datacenters • Highest priority amongst the most up-to-date nodes will be electedStudyBlue, Inc.
    30. 30. Sharding • Horizontal scaling (write scaling) • Automatic data partitioning & balancing • Automatic query routing • Each shard is typically a replica set • Sharding is enabled per collectionStudyBlue, Inc.
    31. 31. Shards • Shard keys define partitions • Names one or more fields that determine how the data is distributed • Order preserving - adjacent data (by key) tend to be stored on the same server • Chunks • Contiguous range of data from a particular collection • Chunks grow to a maximum size, then split into two new chunks • Automatically migrates chunks when chunk count is sufficiently imbalanced between different shardsStudyBlue, Inc.
    32. 32. Sharding ArchitectureStudyBlue, Inc.
    33. 33. Sharding Pieces • Config servers (mongoc) • Stores cluster metadata • Data about each shard and the chunks on that shard • Two-phased commits • Routing servers (mongos) • Receives client requests and routes to appropriate server(s) • Pulls state from config servers • Lightweight - run anywhere (including application servers)StudyBlue, Inc.
    34. 34. MongoDB:ToolsStudyBlue, Inc.
    35. 35. Useful Tools • mongo shell • mongostat • mongotop • Mongo Monitoring Service (MMS)StudyBlue, Inc.
    36. 36. mongo shell• Interactive Javascript shell• Part of the MongoDB installation• Issue commands & queries: ./bin/mongo mydatabase type “help” for help > db mydatabase > person = db.people.findOne( { name : "sara" } ); > person.city = "New York"; > db.people.save( person );StudyBlue, Inc.
    37. 37. mongostat• View statistics on a running mongod instanceStudyBlue, Inc.
    38. 38. mongotop• Track the amount of time a MongoDB instance spends reading and writing data on a per collection basisStudyBlue, Inc.
    39. 39. MMS • Free, cloud-based monitoring service • Charts • Dashboards • Automated alerting • http://mms.10gen.comStudyBlue, Inc.
    40. 40. MongoDB:Strengths &WeaknessesStudyBlue, Inc.
    41. 41. Good At • Horizontal scaling • High availability • Rapidly evolving schemasStudyBlue, Inc.
    42. 42. Less Good At • Drop-in replacement for existing SQL RDBMS • Highly transactional operations (e.g. OLTP) • Ad-hoc business intelligence • Durability without replica sets • Automatic sharding != perfect data distributionStudyBlue, Inc.
    43. 43. Q&AStudyBlue, Inc.
    44. 44. Contact usWeb: http://www.studyblue.comTwitter: @StudyBlueEmail: sean@studyblue.comTwitter: @organicveggie StudyBlue, Inc.

    ×