MongoDB, Ruby on Rails, and Mongoid

6,230 views

Published on

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/

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,230
On SlideShare
0
From Embeds
0
Number of Embeds
774
Actions
Shares
0
Downloads
37
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

MongoDB, Ruby on Rails, and Mongoid

  1. 1. Emily StolfoRuby Engineer at 10gen@EmStolfo
  2. 2. MongoDB and Rails fit together naturally
  3. 3. Ruby engineer on the drivers teamart historian consultant adjunct faculty at Columbia web (Rails) developer
  4. 4. Learning Rails is unconventionalyou have to be resourceful ○ example projects ○ online tutorials ○ community
  5. 5. Rails has conventionsDSL ActiveRecordconvention over rvmconfiguration bundlerblackboxed gems communityDRY MVCAll http stuff was githubwritten already
  6. 6. ActiveRecord● ActiveRecord bridges the gap between OOP and a relational database● mongodb is OO natively● Rails was built for a relational database
  7. 7. 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 isoften favored for the ______ experiencebecause it is ______.
  8. 8. What is Ruby?Ruby was created by ______.Ruby is extremely ______ and ______ touse.It is known for seeing everything as an______ and for the ______ experience.
  9. 9. What is a mongodb driver? 1. connection pooling 2. serialization/deserialization BSON<->hashes 3. wire protocol 4. classes for server stuff ex: abstracts RS, sharded cluster https://github.com/mongodb/mongo-ruby-driver/
  10. 10. @connection = MongoClient.new("localhost", 27017)@data = @connection.db("data")@users = @data.collection("users")
  11. 11. emily = { :name => "Emily", :age => 28, :langs => ["Ruby", "Python", "C++"], :favorites => { :artists => ["George Bellows", "Rodin"], :color => "Blue", :movies => ["Old Boy", "Gladiator"] } }@users.insert(emily)
  12. 12. @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 } )
  13. 13. emily = @users.find_one({:name => "Emily"})emily.langs << "Lisp"@users.save(emily) # too late?@users.update( { :name => "Emily" }, { :$push => { :langs => "Lisp" } } )
  14. 14. @sites.update( {:url => "http://www.nytimes.com"}, { :$inc => { :visits => 1 } }, :upsert => true )
  15. 15. Mongo::MongoReplicaSetClient.new( [localhost:3000, localhost:3001, localhost:3002 ] )Mongo::MongoShardedClient.new( [localhost:3000, localhost:3001, localhost:3002 ])
  16. 16. ODM: Mongoid How do we get Ruby, Rails, and MongoDB to work together?Object Document Mapper available as a gemUsed to be community ODM, with its own driver
  17. 17. User and Favorite model filesclass User include Mongoid::Document field :name, type: String field :age, type: Integer field :langs, type: Array has_many :favoritesendclass Favorite include Mongoid::Document field :artists, type: Array field :color, type: String field :movies, type: Array belongs_to :userend
  18. 18. Docs in MongoDB# 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")}
  19. 19. User and Favorite model filesclass User include Mongoid::Document field :name, type: String field :age, type: Integer field :langs, type: Array embeds_one :favoriteendclass Favorite include Mongoid::Document field :artists, type: Array field :color, type: String field :movies, type: Array embedded_in :userend
  20. 20. Docs in MongoDB# 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"], }}
  21. 21. Thanks! 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 MongoidEmily Stolfo@EmStolfo

×