MongoDB, Ruby on Rails, and Mongoid
-
Upload
emstolfo -
Category
Technology
-
view
4.916 -
download
1
description
Transcript of MongoDB, Ruby on Rails, and Mongoid
![Page 1: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/1.jpg)
Emily Stolfo Ruby Engineer at 10gen@EmStolfo
![Page 2: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/2.jpg)
MongoDB and Rails fit together naturally
![Page 3: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/3.jpg)
Ruby engineer on the drivers team
adjunct faculty at Columbia
web (Rails) developer
consultant
art historian
![Page 4: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/4.jpg)
Learning Rails is unconventional
you have to be resourceful
○ example projects○ online tutorials○ community
![Page 5: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/5.jpg)
Rails has conventions
ActiveRecordrvmbundlergems communityMVCgithub
DSLconvention over configurationblackboxedDRYAll http stuff was written already
![Page 6: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/10.jpg)
@connection = MongoClient.new("localhost", 27017)@data = @connection.db("data")@users = @data.collection("users")
![Page 11: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/12.jpg)
@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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/14.jpg)
@sites.update(
{:url => "http://www.nytimes.com"},
{ :$inc => { :visits => 1 } },
:upsert => true )
![Page 15: MongoDB, Ruby on Rails, and Mongoid](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/18.jpg)
# 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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/20.jpg)
# 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](https://reader033.fdocuments.us/reader033/viewer/2022052315/5555a9dad8b42a52568b486c/html5/thumbnails/21.jpg)
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