Your SlideShare is downloading. ×
0

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Webinar: Getting Started with Ruby and MongoDB

3,676

Published on

This webinar introduces its audience to the basic concepts behind MongoDB and how it fits into the present day Ruby eco-system. We’ll cover the basics of getting started with MongoDB and move on to a …

This webinar introduces its audience to the basic concepts behind MongoDB and how it fits into the present day Ruby eco-system. We’ll cover the basics of getting started with MongoDB and move on to a short introduction to using the MongoDB Ruby driver as well as an introduction to a number of different tools, ODMs and libraries designed to help you seamlessly bring MongoDB into your Ruby applications. This webinar is not an introduction to Ruby itself and it does assume some level of familiarity with Ruby, but will otherwise be a very beginner friendly presentation.

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

No Downloads
Views
Total Views
3,676
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
68
Comments
0
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Webinar: Getting Started with MongoDB and Ruby Brandon Black Software Engineer, MongoDB @brandonmblack
  • 2. +
  • 3. “I believe people want to express themselves when they program. They don't want to fight with the language. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby.” — Yukihiro “Matz” Matsumoto, Creator of Ruby (2001)
  • 4. What is MongoDB?
  • 5. MongoDB is a ___________ database • • • • • Document Open source High performance Horizontally scalable Full featured
  • 6. Document Database • • • • • • Not for .PDF & .DOC files A document is essentially an associative array Document = JSON object Document = PHP Array Document = Python Dict Document = Ruby Hash
  • 7. Open-Source • • • • • • MongoDB is an open source project Available on GitHub Licensed under the AGPL Started & sponsored by MongoDB, Inc. (10gen) Commercial licenses available Contributions welcome
  • 8. High Performance • Written in C++ • Extensive use of memory-mapped files 
 i.e. read-through write-through memory caching. • Runs nearly everywhere • Data serialized as BSON (fast parsing) • Full support for primary & secondary indexes • Document model = less work
  • 9. Shard 1 Shard 2 Shard 3 Horizontally Scalable Shard N
  • 10. Scalability & Performance Data Landscape Memcached MongoDB RDBMS Depth of Functionality
  • 11. Full Featured • • • • • • • Ad Hoc queries Real time aggregation Rich query capabilities Strongly consistent Geospatial features Native support for most programming languages Flexible schema (not schema-less!)
  • 12. Installation & Setup 1 Download From website: http://www.mongodb.org/downloads Package manager: sudo apt-get install mongodb-10gen brew install mongodb 2 Startup MongoDB mongod —dbpath /path/to/data 3 Connect with the mongo Shell Or in our case… IRB/Pry!
  • 13. Using MongoDB with Ruby
  • 14. Using the Ruby Driver Client Application Driver Re a ad Re d Write Secondary Primary Secondary
  • 15. Using the Ruby Driver # install gem and native extensions (optional)! gem install mongo gem install bson_ext require 'mongo'! include Mongo! ! # connecting to the database! client = MongoClient.new # defaults to localhost:27017! client = MongoClient.new('host1.example.com')! client = MongoClient.new('host1.example.com', 27017)! # using configuration options! opts = { :pool_size => 5, :pool_timeout => 5 }! client = MongoClient.new('host1.example.com', 27017, opts)!
  • 16. Using the Ruby Driver seeds = ['h1.example.com:27017',! 'h2.example.com:27017',! 'h3.example.com:27017']! ! # connecting to a replica set! client = MongoReplicaSetClient.new(seeds)! client = MongoReplicaSetClient.new(seeds, :read => :secondary)! # connecting to a sharded cluster! client = MongoShardedClient.new(seeds)! client = MongoShardedClient.new(seeds, :read => :secondary)! # using a connection string! ENV['MONGODB_URI'] = 'mongodb://host1:27017?ssl=true'! client = MongoClient.new!
  • 17. Using the Ruby Driver # using a database! db = client.db('blog')! db = client['blog']! ! client.drop_database('blog')! client.database_names! ! # using a collection! coll = db['posts']! ! coll.drop! db.collection_names!
  • 18. Terminology RDBMS MongoDB Table, View Collection Row Document Index Index Join Embedded Document Foreign Key Reference Partition Shard
  • 19. Building a Blog Models/Entities: • Users (Authors) • Articles • Comments • Tags/Categories
  • 20. In a relational application we would start by defining our schema.
  • 21. Typical Relational Diagram Category ·Name ·URL User ·Name ·Email address Article ·Name ·Slug ·Publish date ·Text Comment ·Comment ·Date ·Author Tag ·Name ·URL
  • 22. In MongoDB we start building and we let the schema evolve as we go. Like Ruby, it has a natural and enjoyable feeling to it!
  • 23. MongoDB Diagram Article User ·Name ·Email address ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value
  • 24. Inserting a New Document # example document! author = {! :username => 'brandonblack',! :first => 'brandon',! :last => 'black'! }! ! # inserting into my blog database! client['blog']['users'].insert(author) No database or collection creation required!
  • 25. Inserting a New Document # example document! article = {! :title => 'Hello World'! :username => 'brandonblack',! :tags => ['ruby', 'getting started'],! :comments => [ # embedded docs ]! }! ! ! # inserting into my blog database! client['blog']['articles'].insert(article) No database or collection creation required!
  • 26. Finding Documents coll = client['blog']['users']! ! # finding a single document! coll.find_one! coll.find_one({ :username => 'brandonblack' })! coll.find_one({ :username => 'brandonblack' }, { :first => 1})! ! coll = client['blog']['articles']! ! # finding multiple documents (using cursors)! cursor = coll.find({ :username => 'brandonblack' }, :limit => 10)! cursor.each do |article|! puts article['title']! end!
  • 27. Updating and Deleting Documents # updating a document! article = { :title => 'Hello Ruby' }! coll.update({ '_id' => article_id }, article)! coll.update({ '_id' => article_id },! {'$set' => {:title => 'Hello Ruby'}})! ! # deleting a single document! coll.remove({ '_id' => article_id })! ! # delete multiple documents! coll.remove({ 'username' => 'brandonblack' })! ! # delete all documents in a collection! coll.remove!
  • 28. Indexes # running explain on a query! coll.find({ :username => 'brandonblack' }).explain! ! # showing collection indexes! coll.index_information! ! # adding an index! coll.ensure_index({ :username => 1 })! coll.ensure_index({ :username => Mongo::ASCENDING })! ! coll.ensure_index({ :username => -1 })! coll.ensure_index({ :username => Mongo::DESCENDING })! ! # adding a special index types! coll.ensure_index({ :loc => Mongo::GEO2D })! coll.ensure_index({ :title => Mongo::TEXT })! ! # droping an index! coll.drop_index('username_1')! ! # dropping all indexes for a collection! coll.drop_indexes!
  • 29. ODMs (Object Document Mappers) Mongoid (http://mongoid.org/) Mongo Mapper (http://mongomapper.com/)
  • 30. Mongoid Example # rails setup! rails g mongoid:config! ! # non-rails setup! Mongoid.load!("path/to/your/mongoid.yml", :production)! ! # document examples! class Article! include Mongoid::Document! field :title, type: String! embeds_many :comments! end! ! class Comment! include Mongoid::Document! field :comment_text, type: String! embedded_in :article! end!
  • 31. What Next?
  • 32. We’ve covered a lot of ground quickly.
  • 33. Come Find Us! • Github • Stack Overflow • MongoDB User Group
  • 34. Further Reading
  • 35. More Ways to Learn Free Online Courses http://education.mongodb.com/ Events & Webinars http://www.mongodb.com/events Presentations http://www.mongodb.com/presentations
  • 36. Thank you! Brandon Black Software Engineer, MongoDB @brandonmblack

×