Download - Rails Loves MongoDB
![Page 1: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/1.jpg)
Rails.loves?(:mongodb) => trueA Peek Into Making Awesomely Dynamic
Applications
![Page 2: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/2.jpg)
How Many Have Experience With Rails?
![Page 3: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/3.jpg)
How Many Have Experience With
Databases?
![Page 4: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/4.jpg)
How Many Have Heard Of MongoDB?
![Page 5: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/5.jpg)
MongoDB Is...Document database (NoSql) (1) Data is stored as BSON Objects (looks exactly like JSON) Gives high performance, high availability, and easy scalability Gives you ultimate flexibility with features like...
Sweet Data Types: Hashes and Arrays Allows you to nest documents Allows for dynamic document attributes
(1) Despite the word “NoSql” you can still create a relational database with MongoDB
![Page 6: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/6.jpg)
MongoDB In Relational TermsMongoDB MySql
Document Row
Collection Table
Database Database
Index Index
![Page 7: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/7.jpg)
More MongoDB KnowledgeTwo types of relations:
Embedded and Referenced No such thing as a “join”, this type of thing can be achieved with embedded documents and linking Has a built in aggregation framework and map reducing support. You still have 1-1, 1-n, n-n relations
![Page 8: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/8.jpg)
What A MongoDB Documents Look Like
![Page 9: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/9.jpg)
How Does Rails Talk To MongoDB?
Mongoid - A Object-Document-Mapper(ODM) (1)
(1) There are other ODM’s out there for MongoDB, MongoMapper for example. I feel Mongoid is far and away the best one. Has a supportive and active maintainer named Durran Jordan.
![Page 10: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/10.jpg)
Why Not Use ActiveRecord?
ActiveRecord does not have built in support for MongoDB. Mongoid replaces it. However, it still uses ActiveModel. You still have all the validation goodness.
![Page 11: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/11.jpg)
Getting StartedInstall MongoDB...Kind of important
Mac:
Windows:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ Ubuntu:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
![Page 12: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/12.jpg)
Getting Started With Rails
Generate a new Rails 4 Application
![Page 13: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/13.jpg)
Modify Your Gemfile
Add the following to your gemfile, then bundle (1)(2)
(1) Current public release of Mongoid doesn’t support Rails 4. To install Mongoid 4.0, point the gem to the Mongoid github project.
!(2) If your using Rails 3, use the public gem
![Page 14: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/14.jpg)
Generate A Mongoid Configuration File
![Page 15: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/15.jpg)
What mongoid.yml Looks Like
![Page 16: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/16.jpg)
All Setup. What Kind of App Should We Make?
Let’s build an app that helps you track video game achievements that you completed.
![Page 17: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/17.jpg)
What Models Do We Need To Create?system: name, short_name game: title, description, released_at, completed_at achievement: title, description, points, multiplayer, completed_at
![Page 18: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/18.jpg)
Let’s Build A Model
Use the Rails generator as normal.
![Page 19: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/19.jpg)
What The Model Looks Like By Default
Clean model just includes Mongoid::Document to a class.
![Page 20: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/20.jpg)
Lets Add Some Fields
In ActiveRecord you use migrations to add and remove fields from tables. In Mongoid you declare your fields right in the model class.
![Page 21: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/21.jpg)
How About Some Validation
![Page 22: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/22.jpg)
Lets Make Some Relations
![Page 23: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/23.jpg)
The Game Model
![Page 24: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/24.jpg)
The Achievement Model
![Page 25: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/25.jpg)
Lets Review What We Just Did
Generated our models Added the fields Added validation Made the relations
![Page 26: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/26.jpg)
Let’s Have Some Fun in Console
![Page 27: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/27.jpg)
Create A System
Validation works
Document Created
![Page 28: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/28.jpg)
Create A Game For Our System
![Page 29: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/29.jpg)
How About Another Game
![Page 30: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/30.jpg)
Let’s Create An Achievement
![Page 31: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/31.jpg)
How About A Multiplayer Achievement
![Page 32: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/32.jpg)
So How Do These Look Stored In The Database?
![Page 33: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/33.jpg)
How Do The Embedded Documents Look?
![Page 34: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/34.jpg)
We Completed One Of Those Achievements, lets update it
![Page 35: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/35.jpg)
Lets Store A Dynamic Field
![Page 36: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/36.jpg)
Lets Review What We Just DidWe created a system called “Xbox One”. We created a new game via our system document, automatically creating the relation to system. We created a new game, then created the relation to system. We can find documents by providing the document ID. Created a single and multiplayer achievement for a game. Took a look and how these are stored in the database. Updated one of the achievements as completed. Updated a document with a dynamic field.
![Page 37: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/37.jpg)
Let’s Try Some Queries
![Page 38: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/38.jpg)
How about all the games that are completed?
![Page 39: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/39.jpg)
How About All Games Who have Achievements With Points Greater than 10?
![Page 40: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/40.jpg)
How About Achievements In A Game With Points Between 10 And 30
![Page 41: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/41.jpg)
Some More Advanced Things You Can Do
Custom Validation Methods Callbacks Scoping GeoNear Queries Map Reducing Aggregation Observers
![Page 42: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/42.jpg)
Some Resources
MongoDB.org - More information about MongoDB RubyonRails.org - Read up on Rails 4 Mongoid.org - Documentation for Mongoid
![Page 43: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/43.jpg)
Follow Me
Github - BillWatts Twitter - @BillWatts
![Page 44: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/44.jpg)
Thank You!Get This Project @ http://github.com/BillWatts/rails_loves_mongodb_talk
![Page 45: Rails Loves MongoDB](https://reader035.vdocuments.net/reader035/viewer/2022062704/556252bbd8b42aa52d8b513d/html5/thumbnails/45.jpg)
Any Questions?