32. Mongo Representation
@alex.to_json
{ name: "alex",
friends: [{ name: "Jim" }, { name: "Bob" }]
}
This is a good thing for scalability, because
JOINS limit our ability to horizontally scale.
33. Mongo Representation
@alex.to_json
{ name: "alex",
friends: [{ name: "Jim" }, { name: "Bob" }]
}
But more importantly, this seems more intuitive
than messing with foreign keys
41. Ok, so what?
We need ORMs to bridge the gap between SQL and native
objects (in our case, Ruby objects)
42. Ok, so what?
I don’t want to map my objects to a schema designed for
querying data.
43. Ok, so what?
I want to store my objects in a datastore that was designed
for storing objects
44. Ok, so what?
Mongo is great for this b/c it stores objects (documents) as
binary JSON
45. Ok, so what?
And as we’ve seen, JSON is great for representing native
objects
46. Ok, so what?
This is important because when I’m writing an application, I
don’t want to accomodate my objects to my datastore.
47. Ok, so what?
I want something flexible that stays out of my way, but
doesn’t sacrifice performance.
48. In a Nutshell
Schema design for humans, not machines
i.e. document-oriented is awesome
schema-less is for adults
Pragmatic balance of performance and functionality
Speed/performance of mongo is super awesome
Scalability features are really powerful too
56. Blogging Application
A post has an author
A post has many tags
A post has many comments
Instead of JOINing separate tables,
we can use embedded documents.
61. Capped collections
Fixed-sized, limited operation, auto age-out
collections (kinda like memcached)
Fixed insertion order
Super fast (faster than normal writes)
Ideal for logging and caching
79. MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
• Very similar syntax to DataMapper
80. MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
• Very similar syntax to DataMapper
• Declarative rather than inheritance-based
81. MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
• Very similar syntax to DataMapper
• Declarative rather than inheritance-based
• Very easy to drop into rails
82. MongoMapper
class Post
include MongoMapper::Document
belongs_to :author, :class_name => "User"
key :title, String, :required => true
key :body, String
key :author_id, Integer, :required => true
key :published_at, Time
key :published, Boolean, :default => false
timestamps!
many :tags
end
class Tag
include MongoMapper::EmbeddedDocument
key :name, String, :required => true
end