21
Emily Stolfo Ruby Engineer at 10gen @EmStolfo

MongoDB, Ruby on Rails, and Mongoid

Embed Size (px)

DESCRIPTION

Keynote from the first Fullstack: Ruby on Rails meetup hosted by General Assembly. Presentation about how Rails and MongoDB fit together naturally and how to use MongoDB with Rails through the Mongoid ODM. http://www.meetup.com/FullStack-General-Assembly-NYC-Ruby-on-Rails/events/107952222/

Citation preview

Page 1: MongoDB, Ruby on Rails, and Mongoid

Emily Stolfo Ruby Engineer at 10gen@EmStolfo

Page 2: MongoDB, Ruby on Rails, and Mongoid

MongoDB and Rails fit together naturally

Page 3: MongoDB, Ruby on Rails, and Mongoid

Ruby engineer on the drivers team

adjunct faculty at Columbia

web (Rails) developer

consultant

art historian

Page 4: MongoDB, Ruby on Rails, and Mongoid

Learning Rails is unconventional

you have to be resourceful

○ example projects○ online tutorials○ community

Page 5: MongoDB, Ruby on Rails, and Mongoid

Rails has conventions

ActiveRecordrvmbundlergems communityMVCgithub

DSLconvention over configurationblackboxedDRYAll http stuff was written already

Page 6: MongoDB, Ruby on Rails, and Mongoid

ActiveRecord

● ActiveRecord bridges the gap between OOP and a relational database

● mongodb is OO natively

● Rails was built for a relational database

Page 7: MongoDB, Ruby on Rails, and Mongoid

what is ?

MongoDB comes from the word ______.

It is a ______ database that is highly ______ and released under the ______ license, making it ______.

It stores data in ______ format and is often favored for the ______ experience because it is ______.

Page 8: MongoDB, Ruby on Rails, and Mongoid

What is Ruby?

Ruby was created by ______.

Ruby is extremely ______ and ______ to use.

It is known for seeing everything as an ______ and for the ______ experience.

Page 9: MongoDB, Ruby on Rails, and Mongoid

1. connection pooling

What is a mongodb driver?

2. serialization/deserialization BSON<->hashes

3. wire protocol

4. classes for server stuffex: abstracts RS, sharded cluster

https://github.com/mongodb/mongo-ruby-driver/

Page 10: MongoDB, Ruby on Rails, and Mongoid

@connection = MongoClient.new("localhost", 27017)@data = @connection.db("data")@users = @data.collection("users")

Page 11: MongoDB, Ruby on Rails, and Mongoid

emily = {

:name => "Emily",

:age => 28,

:langs => ["Ruby", "Python", "C++"],

:favorites => {

:artists => ["George Bellows", "Rodin"],

:color => "Blue",

:movies => ["Old Boy", "Gladiator"]

}

}

@users.insert(emily)

Page 12: MongoDB, Ruby on Rails, and Mongoid

@users.find({:name => "Emily"}) # name = "Emily"

@users.find({:age => {"$lt" => 30 } } ) # age < 30

@users.find({

:age => {"$lt" => 30 }, # ... AND ...

:langs => "Ruby", # look in array

"favorites.color" => "Blue", # reach into objects

} )

Page 13: MongoDB, Ruby on Rails, and Mongoid

emily = @users.find_one({:name => "Emily"})

emily.langs << "Lisp"

@users.save(emily) # too late?

@users.update(

{ :name => "Emily" },

{ :$push => { :langs => "Lisp" } }

)

Page 14: MongoDB, Ruby on Rails, and Mongoid

@sites.update(

{:url => "http://www.nytimes.com"},

{ :$inc => { :visits => 1 } },

:upsert => true )

Page 15: MongoDB, Ruby on Rails, and Mongoid

Mongo::MongoReplicaSetClient.new(

['localhost:3000', 'localhost:3001', 'localhost:3002' ] )

Mongo::MongoShardedClient.new(

['localhost:3000', 'localhost:3001', 'localhost:3002' ] )

Page 16: MongoDB, Ruby on Rails, and Mongoid

ODM: Mongoid

Used to be community ODM, with its own driver

Object Document Mapper available as a gem

How do we get Ruby, Rails, and MongoDB to work together?

Page 17: MongoDB, Ruby on Rails, and Mongoid

class User

include Mongoid::Document

field :name, type: String

field :age, type: Integer

field :langs, type: Array

has_many :favorites

end

class Favorite

include Mongoid::Document

field :artists, type: Array

field :color, type: String

field :movies, type: Array

belongs_to :user

end

User and Favorite model files

Page 18: MongoDB, Ruby on Rails, and Mongoid

# The parent user document

{

"_id" : ObjectId("512e8fdab074c63e71dc6500"),

"name" : "Emily",

"age" : 28,

"langs" : ["Ruby", "Python", "C++"],

}

# The child favorite document

{

"_id" : ObjectId("512e8fdab074c63e71dc6501"),

"artists" : ["George Bellows", "Rodin"],

"color" : "Blue",

"movies" : ["Old Boy", "Gladiator"],

"user_id" : ObjectId("512e8fdab074c63e71dc6500")

}

Docs in MongoDB

Page 19: MongoDB, Ruby on Rails, and Mongoid

class User

include Mongoid::Document

field :name, type: String

field :age, type: Integer

field :langs, type: Array

embeds_one :favorite

end

class Favorite

include Mongoid::Document

field :artists, type: Array

field :color, type: String

field :movies, type: Array

embedded_in :user

end

User and Favorite model files

Page 20: MongoDB, Ruby on Rails, and Mongoid

# The user info and favorites in a single document

{

"_id" : ObjectId("512e8fdab074c63e71dc6500"),

"name" : "Emily",

"age" : 28,

"langs" : ["Ruby", "Python", "C++"],

"favorite" : {

"artists" : ["George Bellows", "Rodin"],

"color" : "Blue",

"movies" : ["Old Boy", "Gladiator"],

}

}

Docs in MongoDB

Page 21: MongoDB, Ruby on Rails, and Mongoid

Keep using resources and the community

Check out the ruby driver and submit a pull request

https://github.com/mongodb/mongo-ruby-driver/

Try out mongoDB and Mongoid

Thanks!

Emily Stolfo @EmStolfo