Document database (NoSql) (1)
Data is stored as BSON Objects (looks exactly like
Gives high performance, high availability, and easy
Gives you ultimate ﬂexibility 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
MongoDB In Relational Terms
More MongoDB Knowledge
Two 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
You still have 1-1, 1-n, n-n relations
How Does Rails Talk To MongoDB?
Mongoid - A Object-DocumentMapper(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.
Why Not Use ActiveRecord?
ActiveRecord does not have built in
support for MongoDB. Mongoid replaces
However, it still uses ActiveModel. You
still have all the validation goodness.
Install MongoDB...Kind of important
Getting Started With Rails
Generate a new Rails 4 Application
Modify Your Gemﬁle
Add the following to your gemﬁle, then
(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
Lets Review What We Just Did
We 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 ﬁnd 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 ﬁeld.