mongodb, ruby on rails, and mongoid

Post on 15-May-2015

4.918 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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/

TRANSCRIPT

Emily Stolfo Ruby Engineer at 10gen@EmStolfo

MongoDB and Rails fit together naturally

Ruby engineer on the drivers team

adjunct faculty at Columbia

web (Rails) developer

consultant

art historian

Learning Rails is unconventional

you have to be resourceful

○ example projects○ online tutorials○ community

Rails has conventions

ActiveRecordrvmbundlergems communityMVCgithub

DSLconvention over configurationblackboxedDRYAll http stuff was written already

ActiveRecord

● ActiveRecord bridges the gap between OOP and a relational database

● mongodb is OO natively

● Rails was built for a relational database

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 ______.

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.

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/

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

emily = {

:name => "Emily",

:age => 28,

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

:favorites => {

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

:color => "Blue",

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

}

}

@users.insert(emily)

@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

} )

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

emily.langs << "Lisp"

@users.save(emily) # too late?

@users.update(

{ :name => "Emily" },

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

)

@sites.update(

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

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

:upsert => true )

Mongo::MongoReplicaSetClient.new(

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

Mongo::MongoShardedClient.new(

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

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?

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

# 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

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

# 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

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

top related