Slide 1

Slide 1 text

Software Engineer, MongoDB @brandonmblack Brandon Black Webinar: Getting Started with MongoDB and Ruby

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

+

Slide 5

Slide 5 text

— Yukihiro “Matz” Matsumoto, Creator of Ruby (2001) “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.”

Slide 6

Slide 6 text

What is MongoDB?

Slide 7

Slide 7 text

MongoDB is a ___________ database • Document • Open source • High performance • Horizontally scalable • Full featured

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Shard N Shard 3 Shard 2 Shard 1 Horizontally Scalable

Slide 12

Slide 12 text

Data Landscape Depth of Functionality Scalability & Performance Memcached MongoDB RDBMS

Slide 13

Slide 13 text

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!)

Slide 14

Slide 14 text

Installation & Setup 1 Download From website: http://www.mongodb.org/downloads sudo apt-get install mongodb-10gen brew install mongodb Package manager: 2 Startup MongoDB 3 Connect with the mongo Shell Or in our case… IRB/Pry! mongod —dbpath /path/to/data

Slide 15

Slide 15 text

Using MongoDB with Ruby

Slide 16

Slide 16 text

Using the Ruby Driver Secondary Secondary Primary Client Application Driver Write Read Read

Slide 17

Slide 17 text

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)!

Slide 18

Slide 18 text

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!

Slide 19

Slide 19 text

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!

Slide 20

Slide 20 text

Terminology RDBMS MongoDB Table, View  Collection Row  Document Index  Index Join  Embedded Document Foreign Key  Reference Partition  Shard

Slide 21

Slide 21 text

Building a Blog Models/Entities: • Users (Authors) • Articles • Comments • Tags/Categories

Slide 22

Slide 22 text

we would start by defining our schema. In a relational application

Slide 23

Slide 23 text

Typical Relational Diagram User ·Name ·Email address Category ·Name ·URL Comment ·Comment ·Date ·Author Article ·Name ·Slug ·Publish date ·Text Tag ·Name ·URL

Slide 24

Slide 24 text

and we let the schema evolve as we go. In MongoDB we start building Like Ruby, it has a natural and enjoyable feeling to it!

Slide 25

Slide 25 text

MongoDB Diagram User ·Name ·Email address Article ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value

Slide 26

Slide 26 text

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!

Slide 27

Slide 27 text

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!

Slide 28

Slide 28 text

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!

Slide 29

Slide 29 text

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!

Slide 30

Slide 30 text

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!

Slide 31

Slide 31 text

ODMs (Object Document Mappers) Mongoid (http://mongoid.org/) Mongo Mapper (http://mongomapper.com/)

Slide 32

Slide 32 text

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!

Slide 33

Slide 33 text

What Next?

Slide 34

Slide 34 text

We’ve covered a lot of ground quickly.

Slide 35

Slide 35 text

Come Find Us! • Github • Stack Overflow • MongoDB User Group

Slide 36

Slide 36 text

Further Reading

Slide 37

Slide 37 text

More Ways to Learn Free Online Courses http://education.mongodb.com/ Events & Webinars http://www.mongodb.com/events Presentations http://www.mongodb.com/presentations

Slide 38

Slide 38 text

Thank you! Software Engineer, MongoDB @brandonmblack Brandon Black